Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseInstagram API (Graph API)
Instagram API(Graph API)
Use the Instagram Graph API by directly executing commands to read and publish Instagram content.
curlOfficial docs:https://developers.facebook.com/docs/instagram-api
通过直接执行命令使用Instagram Graph API来读取和发布Instagram内容。
curl官方文档:https://developers.facebook.com/docs/instagram-api
When to Use
适用场景
Use this skill when you need to:
- Fetch recent media (photos / videos / Reels) from an account
- Get detailed information about a specific media item (caption, type, link, time, etc.)
- Search recent media by hashtag
- Publish image posts via API (with caption)
当你需要以下操作时使用该技能:
- 从账户获取近期媒体内容(照片/视频/Reels)
- 获取特定媒体项的详细信息(说明文字、类型、链接、时间等)
- 按话题标签搜索近期媒体内容
- 通过API发布图片帖子(带说明文字)
Prerequisites
前提条件
- You must have an Instagram Business / Creator account linked to a Facebook Page
- Create an app in Facebook Developers and enable Instagram Basic Display / Instagram Graph API permissions
- Obtain:
- : a long-lived user access token
INSTAGRAM_ACCESS_TOKEN - : your Instagram Business account ID
INSTAGRAM_BUSINESS_ACCOUNT_ID
Set the environment variables, for example:
bash
export INSTAGRAM_ACCESS_TOKEN="EAAG..."
export INSTAGRAM_BUSINESS_ACCOUNT_ID="1784140xxxxxxx"These examples use Graph API version . You can replace this with the latest version if needed.
v21.0- 你必须拥有一个关联到Facebook主页的Instagram商家/创作者账户
- 在Facebook开发者平台创建应用,并启用Instagram Basic Display / Instagram Graph API权限
- 获取:
- :长期用户访问令牌
INSTAGRAM_ACCESS_TOKEN - :你的Instagram商家账户ID
INSTAGRAM_BUSINESS_ACCOUNT_ID
设置环境变量,例如:
bash
export INSTAGRAM_ACCESS_TOKEN="EAAG..."
export INSTAGRAM_BUSINESS_ACCOUNT_ID="1784140xxxxxxx"这些示例使用Graph API版本。如有需要,你可以替换为最新版本。
v21.0Required permissions (scopes)
所需权限(范围)
Depending on which endpoints you use, make sure your app has requested and been approved for (at least):
instagram_basicpages_show_list- (for publishing media)
instagram_content_publish - and related permissions (for insights / some hashtag use cases)
instagram_manage_insights
Important: When usingin a command that pipes to another command, wrap the command containing$VARin$VAR. Due to a Claude Code bug, environment variables are silently cleared when pipes are used directly.bash -c '...'bashbash -c 'curl -s "https://api.example.com" -H "Authorization: Bearer $API_KEY"' | jq '.'
根据你使用的端点,请确保你的应用已申请并获得(至少)以下权限:
instagram_basicpages_show_list- (用于发布媒体内容)
instagram_content_publish - 及相关权限(用于数据洞察/部分话题标签使用场景)
instagram_manage_insights
重要提示: 当在包含管道的命令中使用时,请将包含$VAR的命令用$VAR包裹。由于Claude Code的一个bug,直接使用管道时环境变量会被静默清除。bash -c '...'bashbash -c 'curl -s "https://api.example.com" -H "Authorization: Bearer $API_KEY"' | jq '.'
How to Use
使用方法
All examples below assume you have already set:
bash
INSTAGRAM_ACCESS_TOKEN
INSTAGRAM_BUSINESS_ACCOUNT_ID以下所有示例均假设你已设置好:
bash
INSTAGRAM_ACCESS_TOKEN
INSTAGRAM_BUSINESS_ACCOUNT_ID1. Fetch recent media for the account
1. 获取账户的近期媒体内容
Fetch the most recent media (photos / videos / Reels) for the account:
bash
bash -c 'curl -s -X GET "https://graph.facebook.com/v21.0/${INSTAGRAM_BUSINESS_ACCOUNT_ID}/media?fields=id,caption,media_type,media_url,permalink,timestamp" --header "Authorization: Bearer ${INSTAGRAM_ACCESS_TOKEN}"'Notes:
- Each item in the returned JSON represents a media object
- Common fields:
- : media ID (used for details / insights later)
id - : caption text
caption - :
media_type/IMAGE/VIDEOCAROUSEL_ALBUM - : direct URL to the media
media_url - : Instagram permalink
permalink - : creation time
timestamp
获取账户的最新媒体内容(照片/视频/Reels):
bash
bash -c 'curl -s -X GET "https://graph.facebook.com/v21.0/${INSTAGRAM_BUSINESS_ACCOUNT_ID}/media?fields=id,caption,media_type,media_url,permalink,timestamp" --header "Authorization: Bearer ${INSTAGRAM_ACCESS_TOKEN}"'注意:
- 返回的JSON中的每个条目代表一个媒体对象
- 常见字段:
- :媒体ID(后续用于获取详情/数据洞察)
id - :说明文字
caption - :
media_type/IMAGE/VIDEOCAROUSEL_ALBUM - :媒体内容的直接链接
media_url - :Instagram永久链接
permalink - :创建时间
timestamp
2. Get details for a single media
2. 获取单个媒体内容的详情
If you already have a media , you can fetch more complete information. Replace with the field from the "Get User Media" response (section 1 above):
id<your-media-id>idbash
bash -c 'curl -s -X GET "https://graph.facebook.com/v21.0/<your-media-id>?fields=id,caption,media_type,media_url,permalink,thumbnail_url,timestamp,username" --header "Authorization: Bearer ${INSTAGRAM_ACCESS_TOKEN}"'如果你已有媒体,可以获取更完整的信息。将替换为“获取用户媒体内容”响应(上文第1部分)中的字段:
id<your-media-id>idbash
bash -c 'curl -s -X GET "https://graph.facebook.com/v21.0/<your-media-id>?fields=id,caption,media_type,media_url,permalink,thumbnail_url,timestamp,username" --header "Authorization: Bearer ${INSTAGRAM_ACCESS_TOKEN}"'3. Search media by hashtag
3. 按话题标签搜索媒体内容
Note: hashtag search requires proper business use cases and permissions as defined by Facebook/Instagram. Refer to the official docs.
This usually involves two steps:
注意:话题标签搜索需要符合Facebook/Instagram定义的合理商业使用场景及权限。请参考官方文档。
这通常分为两个步骤:
3.1 Get the hashtag ID
3.1 获取话题标签ID
Replace with any hashtag name you want to search for (without the # symbol), e.g., "travel", "food", "photography":
<hashtag-name>bash
bash -c 'curl -s -X GET "https://graph.facebook.com/v21.0/ig_hashtag_search?user_id=${INSTAGRAM_BUSINESS_ACCOUNT_ID}&q=<hashtag-name>" --header "Authorization: Bearer ${INSTAGRAM_ACCESS_TOKEN}"'Note the field in the returned JSON for use in the next step.
id将替换为你要搜索的话题标签名称(不带#符号),例如“travel”、“food”、“photography”:
<hashtag-name>bash
bash -c 'curl -s -X GET "https://graph.facebook.com/v21.0/ig_hashtag_search?user_id=${INSTAGRAM_BUSINESS_ACCOUNT_ID}&q=<hashtag-name>" --header "Authorization: Bearer ${INSTAGRAM_ACCESS_TOKEN}"'记录返回JSON中的字段,用于下一步操作。
id3.2 Fetch recent media for the hashtag
3.2 获取话题标签的近期媒体内容
Replace with the field from the "Search Hashtag" response (section 3.1 above):
<hashtag-id>idbash
bash -c 'curl -s -X GET "https://graph.facebook.com/v21.0/<hashtag-id>/recent_media?user_id=${INSTAGRAM_BUSINESS_ACCOUNT_ID}&fields=id,caption,media_type,media_url,permalink,timestamp" --header "Authorization: Bearer ${INSTAGRAM_ACCESS_TOKEN}"'将替换为“搜索话题标签”响应(上文第3.1部分)中的字段:
<hashtag-id>idbash
bash -c 'curl -s -X GET "https://graph.facebook.com/v21.0/<hashtag-id>/recent_media?user_id=${INSTAGRAM_BUSINESS_ACCOUNT_ID}&fields=id,caption,media_type,media_url,permalink,timestamp" --header "Authorization: Bearer ${INSTAGRAM_ACCESS_TOKEN}"'4. Publish an image post
4. 发布图片帖子
Publishing an image post via the Graph API usually requires two steps:
- Create a media container
- Publish the container to the feed
通过Graph API发布图片帖子通常需要两个步骤:
- 创建媒体容器
- 将容器发布到动态
4.1 Create a media container
4.1 创建媒体容器
Write the request data to :
/tmp/request.jsonjson
{
"image_url": "https://example.com/image.jpg",
"caption": "Hello from Instagram API 👋"
}Replace with any publicly accessible image URL and update the caption text as needed.
https://example.com/image.jpgbash
bash -c 'curl -s -X POST "https://graph.facebook.com/v21.0/${INSTAGRAM_BUSINESS_ACCOUNT_ID}/media" -H "Content-Type: application/json" -d @/tmp/request.json --header "Authorization: Bearer ${INSTAGRAM_ACCESS_TOKEN}"'The response will contain an (media container ID), for example:
idjson
{
"id": "1790xxxxxxxxxxxx"
}Note this ID for use in the next step.
将请求数据写入:
/tmp/request.jsonjson
{
"image_url": "https://example.com/image.jpg",
"caption": "Hello from Instagram API 👋"
}将替换为任何可公开访问的图片链接,并根据需要更新说明文字。
https://example.com/image.jpgbash
bash -c 'curl -s -X POST "https://graph.facebook.com/v21.0/${INSTAGRAM_BUSINESS_ACCOUNT_ID}/media" -H "Content-Type: application/json" -d @/tmp/request.json --header "Authorization: Bearer ${INSTAGRAM_ACCESS_TOKEN}"'响应将包含一个(媒体容器ID),例如:
idjson
{
"id": "1790xxxxxxxxxxxx"
}记录该ID用于下一步操作。
4.2 Publish the media container to the feed
4.2 将媒体容器发布到动态
Write the request data to :
/tmp/request.jsonjson
{
"creation_id": "<your-creation-id>"
}Replace with the field from the "Create Media Container" response (section 4.1 above):
<your-creation-id>idbash
bash -c 'curl -s -X POST "https://graph.facebook.com/v21.0/${INSTAGRAM_BUSINESS_ACCOUNT_ID}/media_publish" -H "Content-Type: application/json" -d @/tmp/request.json --header "Authorization: Bearer ${INSTAGRAM_ACCESS_TOKEN}"'If successful, the response will contain the final media :
idjson
{
"id": "1791yyyyyyyyyyyy"
}You can then use the "Get details for a single media" command to fetch its .
permalink将请求数据写入:
/tmp/request.jsonjson
{
"creation_id": "<your-creation-id>"
}将替换为“创建媒体容器”响应(上文第4.1部分)中的字段:
<your-creation-id>idbash
bash -c 'curl -s -X POST "https://graph.facebook.com/v21.0/${INSTAGRAM_BUSINESS_ACCOUNT_ID}/media_publish" -H "Content-Type: application/json" -d @/tmp/request.json --header "Authorization: Bearer ${INSTAGRAM_ACCESS_TOKEN}"'如果成功,响应将包含最终的媒体:
idjson
{
"id": "1791yyyyyyyyyyyy"
}之后你可以使用“获取单个媒体内容的详情”命令来获取其。
permalink5. Common errors and troubleshooting
5. 常见错误与故障排除
- Permissions / OAuth errors
- Typical error message:
(#10) Application does not have permission for this action - Check:
- Whether the app has been reviewed / approved
- Whether the required Instagram permissions are enabled
- Whether is a valid long-lived token
INSTAGRAM_ACCESS_TOKEN
- Unsupported account type
- Most Graph API features require Business / Creator accounts
- Make sure the Instagram account type is correct and linked to a Facebook Page
- Rate limits
- Too many requests in a short period may hit rate limits; add delays for bulk operations
- 权限/OAuth错误
- 典型错误信息:
(#10) Application does not have permission for this action - 检查:
- 应用是否已通过审核/获批
- 是否已启用所需的Instagram权限
- 是否为有效的长期令牌
INSTAGRAM_ACCESS_TOKEN
- 不支持的账户类型
- 大多数Graph API功能需要商家/创作者账户
- 确保Instagram账户类型正确且已关联到Facebook主页
- 速率限制
- 短时间内请求过多可能会触发速率限制;批量操作时请添加延迟
Guidelines
注意事项
- Do not log tokens: is sensitive; avoid printing it in logs or chat transcripts
INSTAGRAM_ACCESS_TOKEN - Validate curl commands in a test environment first: confirm flows before wiring them into automation / agents
- Keep API version up to date: periodically check Facebook docs and update the version in URLs to the latest
v21.0 - Use placeholder text for IDs: all examples use placeholder text like instead of shell variables in URLs to avoid dependencies and make examples self-contained
<your-media-id>
- 不要记录令牌:是敏感信息;避免在日志或聊天记录中打印它
INSTAGRAM_ACCESS_TOKEN - 先在测试环境验证curl命令:在将流程接入自动化/代理之前,先确认流程可用
- 保持API版本为最新:定期查看Facebook文档,将URL中的版本更新为最新版本
v21.0 - 使用占位符表示ID:所有示例使用等占位符而非Shell变量,以避免依赖并使示例独立可用
<your-media-id>