Open Instagram API
# Open Instagram API
A self-hosted HTTP API for Instagram, reverse-engineered from the Instagram website.
> Unofficial and not affiliated with Instagram or Meta. Automating an account can get it restricted, so try it on a test account first.
## Setup
1. Export your instagram.com cookies to `cookies.json` (export while logged in; keep that browser logged in).
Check the file with `python3 check_cookies.py cookies.json`.
2. Install and run:
```bash
pip install -r requirements.txt
python3 server.py
```
The API runs at `http://127.0.0.1:8765`. Send the token from `api_token` as `Authorization: Bearer <token>`.
The scripts in `tools/` (for capturing new requests from the website) also need `pip install -r requirements-tools.txt`.
## Endpoints
```
Messages
GET /threads list chats (?folder=requests for message requests)
GET /threads/{id}/messages read messages (?cursor= for older ones)
POST /threads/{id}/messages send {"text": "hi"}
POST /threads/{id}/photos send a photo (multipart: file)
DELETE /threads/{id}/messages/{mid} unsend
POST /threads/{id}/messages/{mid}/reactions react {"emoji": "❤"} (DELETE to remove)
POST /threads/{id}/seen mark as seen
POST /threads/{id}/accept accept a message request
GET /updates?since_ms=0 new incoming messages
Browsing
GET /search?q=nasa users, hashtags, places
GET /users/{username} profile
GET /users/{username}/posts posts (?cursor= for the next page)
GET /users/{username}/reels reels
GET /users/{username}/tagged tagged posts
GET /users/{username}/stories live stories
GET /users/{username}/highlights highlights -> GET /highlights/{id}
GET /users/{username}/followers followers (also /following)
GET /users/{username}/friendship follow status
GET /media/{shortcode} post or reel
GET /media/{shortcode}/comments comments -> GET /comments/{pk}/replies
GET /media/{shortcode}/likers likers
GET /feed/timeline home feed (also /feed/reels, /feed/explore)
GET /stories/tray stories from people you follow
GET /tags/{tag} hashtag posts
GET /locations/{id} location posts
GET /notifications activity
GET /saved saved collections
Actions
POST /media/{shortcode}/like like (DELETE to unlike)
POST /media/{shortcode}/save save (DELETE to unsave)
POST /users/{username}/follow follow (DELETE to unfollow)
POST /media/{shortcode}/comments comment {"text": "nice"}
DELETE /media/{shortcode}/comments/{pk} delete a comment
POST /media/{shortcode}/share send a post/reel to a chat {"thread_id": "..."} or {"users": ["name"]}
Publishing and account
POST /media post 1 image, or 2-10 as a carousel (multipart: files, caption)
PATCH /media/{shortcode} edit caption {"caption": "..."}
GET /account your profile settings
PATCH /account edit {"biography", "first_name", "username"}
PUT /account/privacy {"private": true}
PUT /account/photo set the profile photo (multipart: file; cropped to a square)
DELETE /account/photo remove it
```
Add `?raw=1` to read endpoints to get Instagram's response untouched.
## Realtime
```
GET /events live DM events (Server-Sent Events), new messages in ~0.5 s
GET /events/status connected / disconnected
```
```bash
curl -N -H "Authorization: Bearer $TOKEN" localhost:8765/events
data: {"type": "message", "message": {"id": "...", "kind": "reel", "text": null, "thread_fbid": "...", ...}}
```
The server holds one connection to Instagram's own realtime gateway (the WebSocket instagram.com listens on)
and reconnects by itself.
**Not supported:** blocking/restricting, deleting posts, publishing reels, stories and highlights, editing or muting
messages. Instagram only accepts these from its own website code or app, not from a script. See
[docs/PROTOCOL.md](docs/PROTOCOL.md).
## Staying up to date
Instagram changes its website often, and that can break endpoints. A health check runs every endpoint against a
test account each day; when something breaks, a fix is made and pushed here, usually the same day. If an endpoint
starts failing, pull the latest version (`git pull`) and restart the server.
## Use it from an AI agent
The repository ships a skill that teaches Claude Code, OpenClaw and other agents how to use this API
([skills/open-instagram-api/SKILL.md](skills/open-instagram-api/SKILL.md)).
```bash
/plugin install open-instagram-api --marketplace cprite/open-instagram-api # Claude Code
openclaw skills install @cprite/open-instagram-api # OpenClaw (ClawHub)
npx skills add cprite/open-instagram-api # any agent (skills.sh)
```
### MCP server
`mcp_server.py` gives MCP apps (Claude Desktop, Claude Code, Cursor and others) 33 tools on top of the API: chats and
messages, waiting for new DMs in realtime, profiles, posts, comments, likes, follows, sharing and publishing. Tools that
can't be undone are marked as destructive, so the app asks before using them. The HTTP API keeps working as before;
the MCP server is an optional extra that calls it.
```bash
pip install -r requirements-mcp.txt
python3 server.py # the API must be running
```
Then add it to your MCP app's config:
```json
{"mcpServers": {"instagram": {"command": "python3", "args": ["/path/to/open-instagram-api/mcp_server.py"]}}}
```
It reads the token from `api_token` in the repository folder. `IGDM_API` and `IGDM_TOKEN` override the address and token.
## Examples
- `examples/reel_autoreply.py` auto-replies to reels while you're busy, about a second after they arrive.
- `examples/gpt_command.py`: write `/gpt <question>` in any chat and a short answer from [Codex](https://github.com/openai/codex) appears there. It sees the recent chat and searches the web when needed.
- `examples/traffic_light_pfp.py` turns your profile photo red while you're busy, yellow in between and green when you're free, on a schedule. It can make the three colour versions from your current photo.
## License
MIT
TDQS
Scored across 33 tools
Most tools map to a distinct resource+action, and the DM group (list_threads, unread_threads, get_new_messages, wait_for_messages) is separated by clear descriptions of polling vs. realtime vs. inbox listing. Minor overlap remains between get_profile/get_my_account and get_user_posts/get_post, but descriptions disambiguate them well.
Nearly all tools follow a verb_noun pattern (update_profile, send_message, get_user_posts, like_post, delete_comment, publish_post). A few deviations exist—bare 'search', noun-only 'unread_threads', and verb+adjective 'mark_seen'—but the set is still highly readable and predictable.
33 tools is heavy for a single integration and verges on overwhelming, though the broad Instagram domain (DMs, profiles, feed, content actions, account settings) gives most tools a reason to exist. Some consolidation (e.g. separate photo/message senders, multiple message-fetch tools) would tighten the surface.
Coverage is strong: full DM lifecycle (send, read, react, unsend, accept, seen), profile/social graph, feed/hashtag/search reading, and post interactions. Gaps exist for saved-items listing, story publishing, block/mute, and post deletion (explicitly unsupported), which agents may need to work around.