Skip to content

Latest commit

 

History

History
234 lines (175 loc) · 8.96 KB

README.md

File metadata and controls

234 lines (175 loc) · 8.96 KB

Ampost Python SDK

The official Python client for the Ampost API: publish, schedule and track posts on Instagram, Facebook, Threads, X, YouTube, Pinterest, Bluesky, Telegram and Discord with one call.

  • Typed requests and responses (TypedDicts) for every endpoint, generated from the OpenAPI document
  • Sync and async clients on httpx
  • Retries on 429 and 5xx with Retry-After, automatic Idempotency-Keys, typed errors
  • Pagination by iteration and a one-call media upload
  • Python 3.9+

Install

pip install ampost

The package is on PyPI as ampost.

Quick start

Create an API key in Setup → API keys, connect Bluesky and Threads in the dashboard, then:

import os
from ampost import Ampost

client = Ampost(api_key=os.environ["AMPOST_API_KEY"])

post = client.posts.create({
    "text": "Hello from the Ampost API 👋",
    "targets": [{"platforms": ["bluesky", "threads"]}],
    "publishNow": True,
})
print(post["id"], post["status"], [(p["platform"], p["status"]) for p in post["platformPosts"]])

Leave out publishNow for a draft, or set "scheduledFor": "2026-12-01T09:00:00Z" to schedule it (paid plans). Request bodies and responses are plain dicts with the API's field names; your editor and mypy check them against the typed models in ampost.models.

The async client has the same methods:

import asyncio
from ampost import AsyncAmpost

async def main() -> None:
    async with AsyncAmpost() as client:
        post = await client.posts.create({"text": "Hello", "targets": [{"platforms": ["bluesky"]}]})
        print(post["id"])

asyncio.run(main())

Authentication

Every request sends the key as Authorization: Bearer <key>. Without api_key the client reads AMPOST_API_KEY, and AMPOST_BASE_URL overrides https://api.ampost.io.

client = Ampost(
    api_key="amp_...",
    max_retries=2,   # after a 429, a 5xx or a network error
    timeout=60.0,    # seconds per attempt
)

A key has scopes (posts:write, analytics:read, ...) and can be limited to some platform sets. A call outside them raises PermissionDeniedError with code INSUFFICIENT_SCOPE or PLATFORM_SET_NOT_ALLOWED. See scopes.

Use the client as a context manager (with Ampost() as client:) or call client.close(). To set proxies or transports, pass your own http_client=httpx.Client(...); you close that one yourself.

Errors

Every API error is {"error": {"code", "message", "details"}}, raised as a subclass of APIError:

from ampost import APIError, BadRequestError, RateLimitError

try:
    client.posts.create({"text": "x" * 500, "targets": [{"platforms": ["bluesky"]}]})
except BadRequestError as error:
    print(error.code, error.details)  # CONTENT_TOO_LONG, ...
except RateLimitError as error:
    print(f"retry in {error.retry_after} s")
except APIError as error:
    print(error.status, error.code, error.request_id)
Status Class Typical code
400 BadRequestError VALIDATION_FAILED, CONTENT_TOO_LONG, PLATFORM_NOT_CONNECTED
401 AuthenticationError UNAUTHORIZED
403 PermissionDeniedError INSUFFICIENT_SCOPE, UPGRADE_REQUIRED, PLATFORM_COMING_SOON
404 NotFoundError POST_NOT_FOUND, ACCOUNT_NOT_FOUND
409 ConflictError INVALID_STATUS_TRANSITION, IDEMPOTENCY_KEY_REUSED
413 PayloadTooLargeError PAYLOAD_TOO_LARGE
422 UnprocessableEntityError ANALYTICS_NOT_AVAILABLE_ON_PLATFORM
429 RateLimitError RATE_LIMITED, QUOTA_EXCEEDED
5xx InternalServerError INTERNAL_ERROR, PLATFORM_REQUEST_FAILED

No response at all is APIConnectionError (APITimeoutError after timeout). All of them extend AmpostError. Quote error.request_id when you contact support. Every code is listed in the docs.

A post that was accepted can still fail on one platform: check post["platformPosts"][i]["status"] and ["error"], or subscribe to webhooks.

Idempotency

posts.create, posts.publish, posts.retry, posts.import_csv, accounts.connect, platform_sets.create, webhooks.create and feedback.submit take an Idempotency-Key. The SDK sends a new key with each call and keeps it across that call's retries, so a retried request never posts twice. To stay safe across restarts too (a cron job, a queue worker), pass your own key:

client.posts.create(
    {"text": "v1.4.0 is out", "targets": [{"platforms": ["bluesky"]}], "publishNow": True},
    idempotency_key="release-v1.4.0",
)

The same key with the same request returns the first result for 24 hours; with a different request it is ConflictError IDEMPOTENCY_KEY_REUSED. See idempotency.

Retries

What happened Retried?
429 RATE_LIMITED, QUOTA_EXCEEDED Yes, after Retry-After, if it is under max_retry_delay (60 s)
409 IDEMPOTENCY_REQUEST_IN_PROGRESS Yes
5xx, timeout, network error Reads and calls with an Idempotency-Key only
Anything else No

Waits without Retry-After back off from 0.5 s to 8 s. Set max_retries on the client or per call: client.posts.get(post_id, max_retries=0). Every method also takes timeout and extra_headers.

Pagination

List methods return the first page. Iterate it to get every item, page after page:

page = client.posts.list(status="failed", limit=50)
print(page.data, page.total, page.has_next_page())

for post in client.posts.list(status="failed"):
    client.posts.retry(post["id"])

With AsyncAmpost, await the call for one page or async for over it for every item:

page = await client.posts.list(limit=50)
async for account in client.accounts.list():
    print(account["platform"])
accounts = await client.accounts.list().to_list()

Query parameters are keyword arguments in snake_case: platform_set_id=, and from_= for from.

Media upload

media.upload() takes a path or bytes, sends the file straight to storage (your API key is not sent there), waits until Ampost has checked it and returns a media item:

video = client.media.upload("launch.mp4")
client.posts.create({
    "text": "We launched 🚀",
    "media": [video],
    "targets": [{"platforms": ["youtube", "threads"]}],
    "options": {"youtube": {"title": "Launch day"}},
})

Public URLs work without an upload: "media": [{"url": "https://example.com/a.jpg", "type": "image"}]. For files on a user's device, show uploadPageUrl from media.create_upload() and call media.wait_for_upload(upload_id). See media upload.

Everything else

Each tag of the API is an attribute: posts, accounts, platform_sets (and posting queues), media, analytics, webhooks, platforms, docs, feedback. The types of every request and response are in ampost.models:

from ampost import models

def caption(post: models.Post) -> str:
    return post["text"]

client.platforms.list() returns each platform's limits and options, and client.posts.validate(body) checks a post without creating it.

AI agents can use the same API through the hosted MCP server at https://api.ampost.io/mcp (setup).

Development

The code in src/ampost/_generated/ comes from openapi.json; never edit it by hand. With uv:

uv sync
uv run python scripts/generate.py                                              # from the committed openapi.json
uv run python scripts/generate.py --spec https://api.ampost.io/openapi.json    # refresh the snapshot, then generate
uv run pytest && uv run mypy
AMPOST_API_KEY=amp_... uv run pytest tests/test_smoke.py                       # reads and validates only

Why this generator: openapi-python-client and OpenAPI Generator emit a model class per schema and a module per operation, with their own serialization layer; the result is large and reads unlike the API docs. scripts/generate.py uses only the standard library and turns the spec into TypedDicts (so requests and responses stay the plain JSON dicts the docs show) and one method per operation. The hand-written client (_client.py) adds auth, retries, idempotency, errors and pagination on httpx, the only runtime dependency besides typing_extensions.

Links

License

MIT