Unofficial Python SDK and API server for Meta AI β generate images, chat with Llama, and manage conversations. No API key required, just your browser cookies.
β οΈ Disclaimer: This is an unofficial, reverse-engineered client. It is not affiliated with, endorsed by, or sponsored by Meta. Use at your own risk and in compliance with Meta's Terms of Service. Cookie-based authentication may break at any time as Meta updates their platform.
π Looking for Meta's former AI video creation features?
Meta's AI video creation experience has moved to Vibes.ai. For Vibes.ai video generation, image generation, TTS, lip-sync, timeline editing, media management, and other features, see VibesAI-api.
Meta AI migrated from HTTP GraphQL to DGW (Datagram WebSocket) for all chat and image generation. The old HTTP-based methods no longer work β Meta's GraphQL schema changed (removed RewriteOptionsInput, AttachmentInput, MentionInput, sendMessageStream, and more).
This version uses browser automation (via agent-browser) as the primary method, which works for ANY prompt. The browser creates valid DGW messages natively.
prompt()now uses browser automation (no more OAuth token extraction)generate_image_new()uses browser automation- Video generation is NOT available on Meta AI (Meta says "I can't generate videos right now")
upload_image()is not yet supported via browser method- Cookie-based auth replaces access token for primary methods
- β Image generation (text β image)
- β Chat (text conversation, Instant + Thinking modes)
- β Conversation management (list, new)
- β Media fetching by card ID (HTTP GraphQL)
- β DGW frame replay (advanced WebSocket method)
- πΌοΈ Image generation β Create images from text prompts
- π¬ Chat β Text conversation with Meta AI (Instant + Thinking modes)
- π Conversation management β List conversations
- π Media fetching β Get URLs for already-generated media by card ID
- π No API key β Uses your browser cookies for authentication
- π REST API server β Deploy once, call from any language
- π¦ Python SDK β Use as a library in your Python projects
pip install metaai-sdk[api,browser]
agent-browser install # downloads Chrome for browser automationOr from source:
git clone https://github.com/mir-ashiq/metaai-api.git
cd metaai-api
pip install -e ".[api,browser]"
agent-browser installfrom metaai_api import MetaAI
ai = MetaAI(cookies={
"datr": "your_datr_cookie",
"ecto_1_sess": "your_ecto_1_sess_cookie",
})
# Generate an image
result = ai.generate_image_new("a watercolor painting of a red panda")
print(result["image_urls"])
# Chat
reply = ai.prompt("What is 2+2?")
print(reply["message"])
# List conversations
convs = ai.list_conversations()
for c in convs:
print(c["title"])
ai.close()export META_AI_DATR="your_datr_cookie"
export META_AI_ECTO_1_SESS="your_ecto_1_sess_cookie"
export META_AI_API_KEY="your_secret_key" # optional
python -m metaai_api.api_serverThen call from any language:
curl -X POST http://localhost:8000/image \
-H "Authorization: Bearer your_secret_key" \
-H "Content-Type: application/json" \
-d '{"prompt": "a watercolor painting of a cat"}'- Open https://www.meta.ai/ and log in
- Press F12 β Application β Cookies β
https://www.meta.ai - Copy the values of
datrandecto_1_sess
Or set them as environment variables:
export META_AI_DATR="..."
export META_AI_ECTO_1_SESS="..."See CHANGES_AND_COOKIES.md for details on cookie expiration and the optional access token.
| Method | Description | Status |
|---|---|---|
ai.generate_image_new(prompt) |
Generate image from text | β Works |
ai.prompt(message) |
Send a chat message | β Works |
ai.list_conversations() |
List all conversations | β Works |
ai.new_conversation() |
Start a new conversation | β Works |
ai.fetch_card_media(card_id) |
Fetch media by card ID | β Works |
ai.replay_frame(frame_b64) |
Replay captured DGW frame (advanced) | β Works |
ai.warmup_conversation(conv_id) |
Register a new conversation | β Works |
ai.generate_video_new(prompt) |
Generate video | β Not available |
ai.extend_video(media_id) |
Extend a video | β Not available |
ai.close() |
Close browser session | β Works |
| Endpoint | Method | Description | Status |
|---|---|---|---|
/healthz |
GET | Health check | β |
/chat |
POST | Send a chat message | β |
/image |
POST | Generate image from text | β |
/video |
POST | Generate video | β 501 |
/video/async |
POST | Async video generation | β 501 |
/video/jobs/{job_id} |
GET | Video job status | β 404 |
/video/extend |
POST | Extend a video | β 501 |
/upload |
POST | Upload an image | β 501 |
/conversations |
GET | List conversations | β |
/media |
POST | Fetch media by card ID | β |
/reset |
POST | Reset browser session | β |
/docs |
GET | Swagger UI | β |
Meta AI uses a custom WebSocket protocol called DGW (Datagram WebSocket) for image generation and chat. The server validates message integrity, so you can't construct valid messages from scratch in Python.
This SDK uses browser automation (via agent-browser) to drive a real headless Chrome browser:
- Launches Chrome with your cookies injected
- Types the prompt into the meta.ai chat input
- Waits for the image/response to appear
- Extracts URLs and text from the page
For advanced users, the replay_frame() method allows replaying captured DGW frames via WebSocket (each frame works once).
See GENERATION_API.md for detailed API usage.
export META_AI_DATR="..."
export META_AI_ECTO_1_SESS="..."
export META_AI_API_KEY="..."
docker-compose up -dpip install metaai-sdk[api,browser]
agent-browser install
python -m metaai_api.api_serverSee QUICK_START.md for a step-by-step guide.
- Video generation: NOT available on Meta AI. Meta AI responds: "I can't generate videos right now."
- Cookie expiration:
ecto_1_sessexpires frequently. Re-extract if requests fail. - Speed: ~30-40s per image (browser startup + generation).
- Region: Meta AI isn't available in all regions.
- Image upload: Not yet supported via browser method.
| Variable | Description |
|---|---|
META_AI_DATR |
datr cookie (required) |
META_AI_ECTO_1_SESS |
ecto_1_sess cookie (required) |
META_AI_ABRA_SESS |
abra_sess cookie (optional, some regions) |
META_AI_ACCESS_TOKEN |
DGW access token (for WebSocket method, optional) |
META_AI_API_KEY |
API key for the server (optional) |
META_AI_HEADED |
Show browser window: "true" or "false" (default) |
metaai-api/
βββ src/metaai_api/
β βββ __init__.py # Package exports
β βββ main.py # MetaAI class (main entry point)
β βββ generation.py # GenerationAPI (image/video generation)
β βββ client.py # MetaAIClient (HTTP/WebSocket)
β βββ browser.py # BrowserBackend (agent-browser automation)
β βββ api_server.py # FastAPI server
β βββ parser.py # DGW frame parser
β βββ exceptions.py # Error hierarchy
β βββ utils.py # Constants and helpers
β βββ html_scraper.py # HTML scraping (legacy)
β βββ image_upload.py # Image upload (legacy)
β βββ video_generation.py # Video generation (legacy)
β βββ video_generation_new.py # Video generation (legacy)
βββ examples/ # Example scripts
βββ scripts/ # Test scripts
βββ test/ # Test suite
βββ Dockerfile # Docker deployment
βββ docker-compose.yml # Docker Compose
βββ pyproject.toml # Package config
βββ requirements.txt # Dependencies
MIT
Ashiq Hussain Mir