To start working with the Instagram API in Python, follow these steps:
1. Understand the Instagram Graph API
Instagram provides the Instagram Graph API for developers to access Instagram Business and Creator accounts. It does not support personal accounts directly.
If you’re working with a business account or need analytics, this is the API you should use. For personal accounts, you may need to explore third-party libraries.
2. Set Up Your Developer Account
Create a Facebook App:
Go to the Meta for Developers site.
Create a new app.
Add the “Instagram” product to your app.
Connect Instagram Account:
Link your Instagram Business or Creator account to a Facebook page.
Use the Facebook app to authenticate and access Instagram data.
Get an Access Token:
Generate an access token in the Facebook App Dashboard under the Instagram Graph API.
3. Install Required Libraries
To interact with the API in Python, install requests or use facebook-sdk:
bash Copy code
pip install requests
4. Make API Requests
Here’s an example of fetching basic Instagram account data:
Example Script:
python Copy code
import requests
# Your access token
ACCESS_TOKEN = “your_access_token”
# Base URL for the Instagram Graph API
BASE_URL = “https://graph.facebook.com/v17.0”
# Instagram user ID (you can retrieve this via the API)
USER_ID = “your_user_id”
# Endpoint to get Instagram account information
endpoint = f”{BASE_URL}/{USER_ID}?fields=id,username&access_token={ACCESS_TOKEN}”
# Make the API request
response = requests.get(endpoint)
# Print the result
if response.status_code == 200:
data = response.json()
print(“Instagram Account Info:”)
print(data)
else:
print(f”Error: {response.status_code}”)
print(response.json())
5. Using Python Libraries
Alternatively, use libraries like InstagramAPI or instabot for simplified functionality, but keep in mind:
Third-party libraries may have limitations or compliance issues with Instagram’s policies.
Always use official APIs for production apps.
6. Important Notes
The Instagram Graph API primarily supports Business and Creator accounts.
You’ll need proper permissions (like instagram_basic) for your app.
Check Instagram Graph API Documentation for details on endpoints and limits.