Instagram API Client Library
A Python library for interacting with the official Instagram Graph API. This client provides easy-to-use methods for authentication, user management, conversations, and messaging functionality.
Official instagram documentation
Demo
You can visit robosell to test how instagram integration works and features have
Features
- OAuth 2.0 Authentication
- Long-lived token exchange
- User profile management
- Conversation handling
- Message management
- Built-in pagination support
Installation
pip install instagram-client
Prerequisites
Before using this library, you need to:
- Create an Instagram App in the Meta for Developers Console
- Obtain your Client ID and Client Secret
- Configure your OAuth redirect URI
- Set up the required scopes for your application
Quick Start
from instagram_client import InstagramClient
# Initialize the client
client = InstagramClient(
client_id="your_client_id",
client_secret="your_client_secret",
redirect_uri="your_redirect_uri",
scopes=["instagram_business_basic", "instagram_business_manage_messages"]
)
# For more detailed information visit https://developers.facebook.com/docs/instagram-platform/instagram-api-with-instagram-login
# Get the authorization URL
auth_url = client.get_auth_url()
# Also you can pass custom data to oauth url:
# auth_url = client.get_auth_url(state="user_{user_id}")
# Redirect user to auth_url for authorization
# After user authorization, exchange the code for an access token
login_response = client.retrieve_access_token(code="authorization_code")
# Exchange for a long-lived token
long_lived_token = client.exchange_long_lived_token() # long lived token expires in 60 days and short token expires in 30min
# Get authenticated user's profile
user_profile = client.get_me()
Detailed Usage
Authentication Flow
# Create new endpoint to redirect oauth url
# 1. Generate authentication URL
auth_url = client.get_auth_url(
enable_fb_login=1,
force_authentication=0,
state="optional_state_parameter"
)
# 2. After user authorization, exchange the code for an access token
login_response = client.retrieve_access_token(code="authorization_code")
print(f"Access Token: {login_response.access_token}")
# 3. Exchange for a long-lived token
long_lived_token = client.exchange_long_lived_token()
print(f"Long-lived Token: {long_lived_token.access_token}")
print(f"Token Expires in: {long_lived_token.expires_in} seconds")
User Management
# Get authenticated user's profile
me = client.get_me()
print(f"Username: {me.username}")
print(f"Followers: {me.followers_count}")
print(f"Following: {me.follows_count}")
# Get another user's profile
user_profile = client.get_profile_info(user_id="target_user_id")
print(f"Name: {user_profile.name}")
print(f"Is Following Business: {user_profile.is_user_follow_business}")
Conversation Management
# Get all conversations
conversations = client.get_conversations()
for conversation in conversations:
print(f"Conversation ID: {conversation.id}")
print(f"Participants: {conversation.participants}")
# Get conversation with specific user
user_conversations = client.get_user_conversation(
user_id="target_user_id")
# Get messages from a conversation
messages = client.get_conversation_messages(
conversation_id="conversation_id",
desired_limit=100 # Optional: limit number of messages
)
for message in messages:
print(f"From: {message.from_}")
print(f"Message: {message.message}")
print(f"Time: {message.created_time}")
Sending Messages
from instagram_client.schemes.conversation_schemes import SendMessageScheme
# Create message payload
message = SendMessageScheme(
message="Hello from Instagram API Client!",
recipient_id="target_user_id"
)
# Send message
response = client.send_message(
instagram_id="target_user_id",
payload=message
)
Error Handling
The library includes built-in error handling for common API issues:
from instagram_client.exceptions.http_exceptions import ForbiddenApiException
try:
token = client.retrieve_access_token(code="invalid_code")
except ForbiddenApiException as e:
print(f"Authentication failed: {e}")
API Documentation
For detailed information about the Instagram Graph API endpoints and features, visit the official Instagram Graph API documentation.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Security
Please note that this library handles sensitive authentication tokens. Always follow security best practices:
- Never commit tokens to version control
- Store sensitive credentials in environment variables
- Use secure methods to handle and store access tokens
- Implement proper token refresh mechanisms
Support
If you encounter any issues or have questions, please file an issue on the GitHub repository.
Contacts:
Email - erkinovabdulvoris101@gmail.com
Telegram - @abdulvoris_101
Metadata
Release files for instagram-client 0.2.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| instagram_client-0.2.5.tar.gz | 10.7 kB | Without provenance | Details for instagram_client-0.2.5.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| instagram_client-0.2.5-py3-none-any.whl | Python 3 | none | any | Details for instagram_client-0.2.5-py3-none-any.whl |
Total release size: 21.7 kB
Release files / instagram_client-0.2.5.tar.gz
| Download URL | instagram_client-0.2.5.tar.gz |
|---|---|
| Size | 10.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
11cd3ba28e7be376b5b06f948f3dd3cf2557e1ab3bd10b98bc8fd7ce5e8de7b3
|
|
BLAKE2b-256 checksum How to use checksums |
61e02a0057f9d11773dbea2a49b0472ef25ab6d3ff94478492d0a63cc8f6f203
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.9
|
Release files / instagram_client-0.2.5-py3-none-any.whl
| Download URL | instagram_client-0.2.5-py3-none-any.whl |
|---|---|
| Size | 10.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
67f74914859b1dd85a844aa6ccca3e941ab3bbfe914553944da775803c684ee9
|
|
BLAKE2b-256 checksum How to use checksums |
54b7e436987da325e7b896734f774d18ca1cfb7f2c1bf3180b87003b3a248371
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.9
|
Security for instagram-client 0.2.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.