Streamline Instagram User and Media Management with API Actions

The Instagram User and Media Management API offers developers a powerful suite of tools to interact with Instagram's vast user and media data. By leveraging these Cognitive Actions, you can efficiently manage user profiles, retrieve media comments, and access user information based on their unique identifiers. This API simplifies the process of engaging with Instagram data, allowing developers to create more dynamic applications that enhance user experience.
Whether you're building social media analytics tools, content management systems, or enhancing user interactions within your own applications, these actions can save you time and effort. Common use cases include fetching user profiles for display, retrieving comments for moderation or analysis, and searching for users based on specific queries. This API provides the flexibility needed to integrate Instagram functionalities seamlessly into your projects.
Prerequisites
To get started, you will need an API key for the Cognitive Actions service and a basic understanding of making API calls.
Fetch Instagram Profile by User ID
This action allows you to retrieve an Instagram user's profile information using their unique Instagram user ID. It is essential for applications that need to display user details or verify user identities.
- Input Requirements: The request must include a
userIdentifier, which is the unique identifier assigned to the user. This value is mandatory.- Example Input:
{ "userIdentifier": "2278169415" }
- Example Input:
- Expected Output: The output includes various user profile details such as username, biography, follower count, and profile picture URL.
- Example Output:
{ "pk": 2278169415, "username": "mrbeast", "full_name": "MrBeast", "follower_count": 75712546, "profile_pic_url": "https://scontent-fra5-1.cdninstagram.com/v/t51.2885-19/31077884_211593632905749_1394765701385814016_n.jpg?stp=dst-jpg_e0_s150x150_tt6&_nc_ht=scontent-fra5-1.cdninstagram.com&_nc_cat=1&_nc_oc=Q6cZ2QEBiV_AYzR1WfOitzXFvIW_pTqojpCo6YBEXuSMUPZSZVmThTao1vnxyNUmvxZcHCI&_nc_ohc=Bi60YfIor4kQ7kNvwE0lZte&_nc_gid=j_WTc6FAJ94mdMHnGvThmg&edm=AEF8tYYBAAAA&ccb=7-5&ig_cache_key=GPw12gEVelN7ccAAAAAAAAAENFsTbkULAAAB-ccb7-5&oh=00_AfSSk_MWq59ezZe2u87oUZLVkSnQ0vHVP9vG7-nBFNjExg&oe=687CD24F&_nc_sid=1e20d2" }
- Example Output:
- Use Cases for this Action: This action is particularly useful for applications that need to display user information, such as social media dashboards, user verification systems, or any application that requires user profile data.
```python
import requests
import json
# Replace with your actual Cognitive Actions API key and endpoint
# Ensure your environment securely handles the API key
COGNITIVE_ACTIONS_API_KEY = "YOUR_COGNITIVE_ACTIONS_API_KEY"
# This endpoint URL is hypothetical and should be documented for users
COGNITIVE_ACTIONS_EXECUTE_URL = "https://api.cognitiveactions.com/actions/execute"
action_id = "1a0e2a5d-4609-4e09-a28a-d6cd146363dd" # Action ID for: Fetch Instagram Profile by User ID
# Construct the exact input payload based on the action's requirements
# This example uses the predefined example_input for this action:
payload = {
"userIdentifier": "2278169415"
}
headers = {
"Authorization": f"Bearer {COGNITIVE_ACTIONS_API_KEY}",
"Content-Type": "application/json",
# Add any other required headers for the Cognitive Actions API
}
# Prepare the request body for the hypothetical execution endpoint
request_body = {
"action_id": action_id,
"inputs": payload
}
print(f"--- Calling Cognitive Action: {action.name or action_id} ---")
print(f"Endpoint: {COGNITIVE_ACTIONS_EXECUTE_URL}")
print(f"Action ID: {action_id}")
print("Payload being sent:")
print(json.dumps(request_body, indent=2))
print("------------------------------------------------")
try:
response = requests.post(
COGNITIVE_ACTIONS_EXECUTE_URL,
headers=headers,
json=request_body
)
response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
result = response.json()
print("Action executed successfully. Result:")
print(json.dumps(result, indent=2))
except requests.exceptions.RequestException as e:
print(f"Error executing action {action_id}: {e}")
if e.response is not None:
print(f"Response status: {e.response.status_code}")
try:
print(f"Response body: {e.response.json()}")
except json.JSONDecodeError:
print(f"Response body (non-JSON): {e.response.text}")
print("------------------------------------------------")
## Fetch Instagram Media Comments
With this action, you can retrieve comments from Instagram media using a unique media identifier. It supports pagination through comments, allowing for efficient data retrieval.
- **Input Requirements:** The request must include an `id`, which is the unique string identifier for the media. An optional `nextMinimumId` can be included for pagination.
- Example Input:
```json
{
"id": "3659391374559839956_8663171404"
}
```
- **Expected Output:** The output will return the status, the media caption, and a list of comments associated with the media.
- Example Output:
```json
{
"status": "ok",
"comments": [
{
"pk": 17943862611007632,
"text": "😍",
"user": {
"pk": 66998519450,
"username": "iskaa_aaiiddiyaakyaahaiiskaa"
}
}
]
}
```
- **Use Cases for this Action:** This action is ideal for applications that need to analyze user engagement on posts, moderate comments, or display comments for user interaction features.
import requests
import json
# Replace with your actual Cognitive Actions API key and endpoint
# Ensure your environment securely handles the API key
COGNITIVE_ACTIONS_API_KEY = "YOUR_COGNITIVE_ACTIONS_API_KEY"
# This endpoint URL is hypothetical and should be documented for users
COGNITIVE_ACTIONS_EXECUTE_URL = "https://api.cognitiveactions.com/actions/execute"
action_id = "1ed7a3ca-6f28-4a88-b447-8506b53491c5" # Action ID for: Fetch Instagram Media Comments
# Construct the exact input payload based on the action's requirements
# This example uses the predefined example_input for this action:
payload = {
"id": "3659391374559839956_8663171404"
}
headers = {
"Authorization": f"Bearer {COGNITIVE_ACTIONS_API_KEY}",
"Content-Type": "application/json",
# Add any other required headers for the Cognitive Actions API
}
# Prepare the request body for the hypothetical execution endpoint
request_body = {
"action_id": action_id,
"inputs": payload
}
print(f"--- Calling Cognitive Action: {action.name or action_id} ---")
print(f"Endpoint: {COGNITIVE_ACTIONS_EXECUTE_URL}")
print(f"Action ID: {action_id}")
print("Payload being sent:")
print(json.dumps(request_body, indent=2))
print("------------------------------------------------")
try:
response = requests.post(
COGNITIVE_ACTIONS_EXECUTE_URL,
headers=headers,
json=request_body
)
response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
result = response.json()
print("Action executed successfully. Result:")
print(json.dumps(result, indent=2))
except requests.exceptions.RequestException as e:
print(f"Error executing action {action_id}: {e}")
if e.response is not None:
print(f"Response status: {e.response.status_code}")
try:
print(f"Response body: {e.response.json()}")
except json.JSONDecodeError:
print(f"Response body (non-JSON): {e.response.text}")
print("------------------------------------------------")
## Retrieve User ID from Instagram Username
This action allows you to fetch the Instagram user ID by using the username, which is particularly helpful for applications that require user identification without prior knowledge of their IDs.
- **Input Requirements:** The request must include a `username`, which defaults to "instagram" if not provided.
- Example Input:
```json
{
"username": "instagram"
}
```
- **Expected Output:** The output includes the user ID and the status of the request.
- Example Output:
```json
{
"data": 25025320,
"status": "success"
}
```
- **Use Cases for this Action:** This action is useful for user lookup features in applications, enabling smooth integration of user data without requiring users to provide their IDs directly.
import requests
import json
# Replace with your actual Cognitive Actions API key and endpoint
# Ensure your environment securely handles the API key
COGNITIVE_ACTIONS_API_KEY = "YOUR_COGNITIVE_ACTIONS_API_KEY"
# This endpoint URL is hypothetical and should be documented for users
COGNITIVE_ACTIONS_EXECUTE_URL = "https://api.cognitiveactions.com/actions/execute"
action_id = "360615e6-e2fe-4f42-8a6e-18173b1de621" # Action ID for: Retrieve User ID from Instagram Username
# Construct the exact input payload based on the action's requirements
# This example uses the predefined example_input for this action:
payload = {
"username": "instagram"
}
headers = {
"Authorization": f"Bearer {COGNITIVE_ACTIONS_API_KEY}",
"Content-Type": "application/json",
# Add any other required headers for the Cognitive Actions API
}
# Prepare the request body for the hypothetical execution endpoint
request_body = {
"action_id": action_id,
"inputs": payload
}
print(f"--- Calling Cognitive Action: {action.name or action_id} ---")
print(f"Endpoint: {COGNITIVE_ACTIONS_EXECUTE_URL}")
print(f"Action ID: {action_id}")
print("Payload being sent:")
print(json.dumps(request_body, indent=2))
print("------------------------------------------------")
try:
response = requests.post(
COGNITIVE_ACTIONS_EXECUTE_URL,
headers=headers,
json=request_body
)
response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
result = response.json()
print("Action executed successfully. Result:")
print(json.dumps(result, indent=2))
except requests.exceptions.RequestException as e:
print(f"Error executing action {action_id}: {e}")
if e.response is not None:
print(f"Response status: {e.response.status_code}")
try:
print(f"Response body: {e.response.json()}")
except json.JSONDecodeError:
print(f"Response body (non-JSON): {e.response.text}")
print("------------------------------------------------")
## Fetch Instagram Users by Query
This action allows you to retrieve a list of Instagram users based on a specific search query. It is beneficial for applications that need to implement a search feature for users.
- **Input Requirements:** The request must include a `query`, which is the search term to find users.
- Example Input:
```json
{
"query": "mrbeast"
}
```
- **Expected Output:** The output will return a list of users matching the query, including their usernames and profile information.
- Example Output:
```json
[
{
"pk": 2278169415,
"username": "mrbeast",
"full_name": "MrBeast",
"is_verified": true
}
]
```
- **Use Cases for this Action:** This action is particularly useful in social discovery applications, enabling users to search for and find other users on Instagram easily.
import requests
import json
# Replace with your actual Cognitive Actions API key and endpoint
# Ensure your environment securely handles the API key
COGNITIVE_ACTIONS_API_KEY = "YOUR_COGNITIVE_ACTIONS_API_KEY"
# This endpoint URL is hypothetical and should be documented for users
COGNITIVE_ACTIONS_EXECUTE_URL = "https://api.cognitiveactions.com/actions/execute"
action_id = "46ce4d72-dae3-4bdd-9db6-7411ab7c9f67" # Action ID for: Fetch Instagram Users by Query
# Construct the exact input payload based on the action's requirements
# This example uses the predefined example_input for this action:
payload = {
"query": "mrbeast"
}
headers = {
"Authorization": f"Bearer {COGNITIVE_ACTIONS_API_KEY}",
"Content-Type": "application/json",
# Add any other required headers for the Cognitive Actions API
}
# Prepare the request body for the hypothetical execution endpoint
request_body = {
"action_id": action_id,
"inputs": payload
}
print(f"--- Calling Cognitive Action: {action.name or action_id} ---")
print(f"Endpoint: {COGNITIVE_ACTIONS_EXECUTE_URL}")
print(f"Action ID: {action_id}")
print("Payload being sent:")
print(json.dumps(request_body, indent=2))
print("------------------------------------------------")
try:
response = requests.post(
COGNITIVE_ACTIONS_EXECUTE_URL,
headers=headers,
json=request_body
)
response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
result = response.json()
print("Action executed successfully. Result:")
print(json.dumps(result, indent=2))
except requests.exceptions.RequestException as e:
print(f"Error executing action {action_id}: {e}")
if e.response is not None:
print(f"Response status: {e.response.status_code}")
try:
print(f"Response body: {e.response.json()}")
except json.JSONDecodeError:
print(f"Response body (non-JSON): {e.response.text}")
print("------------------------------------------------")
## Fetch Instagram Comments by Highlight ID
This action retrieves comments from Instagram media based on the unique identifier for media highlights. It allows developers to access specific comment threads related to highlights.
- **Input Requirements:** The request must include an `id`, which is the unique identifier for the highlight.
- Example Input:
```json
{
"id": "18012869968809469"
}
```
- **Expected Output:** The output includes the comments associated with the highlight, along with relevant user information.
- Example Output:
```json
{
"status": "ok",
"reels": {
"highlight:18012869968809469": {
"items": [
{
"id": "2934378296838617130_2278169415",
"text": "Great highlight!",
"user": {
"username": "mrbeast"
}
}
]
}
}
}
```
- **Use Cases for this Action:** This action is beneficial for applications that focus on user engagement and interaction with highlights, allowing for better content management and user feedback.
import requests
import json
# Replace with your actual Cognitive Actions API key and endpoint
# Ensure your environment securely handles the API key
COGNITIVE_ACTIONS_API_KEY = "YOUR_COGNITIVE_ACTIONS_API_KEY"
# This endpoint URL is hypothetical and should be documented for users
COGNITIVE_ACTIONS_EXECUTE_URL = "https://api.cognitiveactions.com/actions/execute"
action_id = "5ef5053e-f34b-49ec-94ac-7af0bd4fbbee" # Action ID for: Fetch Instagram Comments by Highlight ID
# Construct the exact input payload based on the action's requirements
# This example uses the predefined example_input for this action:
payload = {
"id": "18012869968809469"
}
headers = {
"Authorization": f"Bearer {COGNITIVE_ACTIONS_API_KEY}",
"Content-Type": "application/json",
# Add any other required headers for the Cognitive Actions API
}
# Prepare the request body for the hypothetical execution endpoint
request_body = {
"action_id": action_id,
"inputs": payload
}
print(f"--- Calling Cognitive Action: {action.name or action_id} ---")
print(f"Endpoint: {COGNITIVE_ACTIONS_EXECUTE_URL}")
print(f"Action ID: {action_id}")
print("Payload being sent:")
print(json.dumps(request_body, indent=2))
print("------------------------------------------------")
try:
response = requests.post(
COGNITIVE_ACTIONS_EXECUTE_URL,
headers=headers,
json=request_body
)
response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
result = response.json()
print("Action executed successfully. Result:")
print(json.dumps(result, indent=2))
except requests.exceptions.RequestException as e:
print(f"Error executing action {action_id}: {e}")
if e.response is not None:
print(f"Response status: {e.response.status_code}")
try:
print(f"Response body: {e.response.json()}")
except json.JSONDecodeError:
print(f"Response body (non-JSON): {e.response.text}")
print("------------------------------------------------")
## Retrieve Instagram Username by User ID
This action allows you to fetch the Instagram username associated with a given user ID. It facilitates the mapping of user IDs to their respective usernames, enhancing user identification processes.
- **Input Requirements:** The request must include a `userIdentifier`, which is a string uniquely identifying the user.
- Example Input:
```json
{
"userIdentifier": "25025320"
}
```
- **Expected Output:** The output will return the user ID and the corresponding username.
- Example Output:
```json
{
"UserID": 25025320,
"UserName": "instagram"
}
```
- **Use Cases for this Action:** This action is particularly useful in applications that require user mapping, such as analytics tools and user management systems.
import requests
import json
# Replace with your actual Cognitive Actions API key and endpoint
# Ensure your environment securely handles the API key
COGNITIVE_ACTIONS_API_KEY = "YOUR_COGNITIVE_ACTIONS_API_KEY"
# This endpoint URL is hypothetical and should be documented for users
COGNITIVE_ACTIONS_EXECUTE_URL = "https://api.cognitiveactions.com/actions/execute"
action_id = "db0b906b-9b0d-41ed-8063-7b5ebd0014f6" # Action ID for: Retrieve Instagram Username by User ID
# Construct the exact input payload based on the action's requirements
# This example uses the predefined example_input for this action:
payload = {
"userIdentifier": "25025320"
}
headers = {
"Authorization": f"Bearer {COGNITIVE_ACTIONS_API_KEY}",
"Content-Type": "application/json",
# Add any other required headers for the Cognitive Actions API
}
# Prepare the request body for the hypothetical execution endpoint
request_body = {
"action_id": action_id,
"inputs": payload
}
print(f"--- Calling Cognitive Action: {action.name or action_id} ---")
print(f"Endpoint: {COGNITIVE_ACTIONS_EXECUTE_URL}")
print(f"Action ID: {action_id}")
print("Payload being sent:")
print(json.dumps(request_body, indent=2))
print("------------------------------------------------")
try:
response = requests.post(
COGNITIVE_ACTIONS_EXECUTE_URL,
headers=headers,
json=request_body
)
response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
result = response.json()
print("Action executed successfully. Result:")
print(json.dumps(result, indent=2))
except requests.exceptions.RequestException as e:
print(f"Error executing action {action_id}: {e}")
if e.response is not None:
print(f"Response status: {e.response.status_code}")
try:
print(f"Response body: {e.response.json()}")
except json.JSONDecodeError:
print(f"Response body (non-JSON): {e.response.text}")
print("------------------------------------------------")
### Conclusion
Integrating the Instagram User and Media Management API into your applications opens up a world of possibilities for user engagement and media management. Each Cognitive Action provides unique functionalities that can streamline processes, enhance user experiences, and allow developers to build more interactive and user-friendly applications.
To get started, obtain your API key and explore each action to see how they can fit into your project. Whether it's fetching user profiles, managing comments, or searching for users, these tools will empower you to create dynamic and responsive applications that leverage Instagram's rich ecosystem.