Skip to main content

emailcentralpy

Python SDK for the Email Central API.

Install

pip install emailcentralpy

Requires Python 3.10+. The only dependency is requests.

Quickstart

Construct a client

from emailcentral import Client

client = Client(api_key="YOUR_API_KEY")

By default the client talks to https://email-central.net/api/v1. Pass base_url= to point it elsewhere (e.g. a staging environment):

client = Client(api_key="YOUR_API_KEY", base_url="https://staging.email-central.net/api/v1")

List products

products = client.list_products()
for p in products:
    print(p.id, p.name, p.price_usd, p.stock)

price_mills on Product is the raw price in mills (1/1000 of a dollar) as returned by the API. price_usd is a convenience property that divides it by 1000.

Check balance

cents = client.balance()
print(f"${cents / 100:.2f}")

Buy accounts

result = client.buy(product_id=1, quantity=5)
print(result.purchase_id, result.new_balance_cents)
for item in result.items:
    print(item)

To make a purchase safely retryable (e.g. if your process crashes or the connection drops mid-request), pass an idempotency_key. Reusing the same key returns the original purchase result instead of charging again:

result = client.buy(product_id=1, quantity=5, idempotency_key="order-2026-08-05-001")

If you reuse a key while the original call is still in flight, the API responds with 409 request_in_progress, which surfaces as an APIError with code == "request_in_progress".

Each string in result.items is one purchased account, in the form email:password or, for Graph-capable products, email:password:refreshToken:clientId.

List transactions

page = client.transactions(page=1)
for tx in page.transactions:
    print(tx.order_no, tx.amount_cents, tx.status)
print(page.page, page.total_pages, page.total)

Handling errors

Any non-2xx response raises emailcentral.APIError, with status (HTTP status code), code (the API's error string), and message:

from emailcentral import APIError

try:
    client.buy(product_id=999, quantity=1)
except APIError as e:
    print(e.status, e.code, e.message)

Reading a Graph inbox

If a purchased item came from a Graph-capable product, its string includes a refresh token and client ID that let you read the mailbox directly through Microsoft Graph. This path never touches the Email Central API or your API key.

from emailcentral import parse_account, exchange_token, read_inbox

result = client.buy(product_id=1, quantity=1)
account = parse_account(result.items[0])

access_token = exchange_token(account.client_id, account.refresh_token)

messages = read_inbox(access_token, folder="inbox", limit=10)
for m in messages:
    print(m.date, m.from_, m.subject)

folder is one of "inbox", "junk", or "all".

Note: on Message, the sender field is named from_ (with a trailing underscore) because from is a reserved word in Python.

parse_account splits on : and takes the email as the first field and the client ID as the last field; everything in between is rejoined with : as the refresh token, since refresh tokens can themselves contain colons. It raises ValueError if the string doesn't have at least 4 :-separated parts.

Keeping a Graph token from expiring

A refresh token is valid for 90 days from when the account was created. Every exchange against Microsoft's official token endpoint issues a brand new refresh token and resets that window, so calling refresh_account_token periodically - even on accounts you're not actively reading mail from - keeps them alive indefinitely.

from emailcentral import refresh_account_token

refreshed = refresh_account_token(account.client_id, account.refresh_token)
# refreshed.access_token can be discarded if you're just keeping the account alive
# refreshed.refresh_token replaces the old one - the old one is now invalid

Run this on a schedule (daily or weekly is plenty) well before the 90-day mark for any account you're holding onto. Each exchange invalidates the refresh token that was passed in, so the value you get back must be what you use next time.

Examples

A complete, runnable script covering every method (list_products, balance, transactions, orders, buy, order_items, and the Graph flow) is in examples/quickstart.py. It reads the API key from the EMAILCENTRAL_API_KEY environment variable:

EMAILCENTRAL_API_KEY=your-key python examples/quickstart.py

Error codes

These are the code values you may see on a raised APIError, with the HTTP status they come with.

Status Code Meaning
401 unauthorized API key missing, invalid, or revoked
400 invalid_body Request body failed validation (e.g. bad productId/quantity)
415 unsupported_media_type Request Content-Type was not accepted
413 payload_too_large Request body exceeded the size limit
404 product_not_found No product exists with the given productId
400 insufficient_balance Account balance too low to complete the purchase
400 insufficient_stock Not enough stock left for the requested quantity
400 purchase_failed Purchase could not be completed for another reason
409 request_in_progress The same Idempotency-Key is already being processed
500 internal_error Server-side error
429 rate_limited Too many requests; back off and retry

unauthorized and rate_limited can be returned by every endpoint (list_products, buy, balance, transactions), not just buy.

Metadata

Release files for emailcentralpy 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for emailcentralpy 1.1.0
File Size Uploaded Provenance Actions
emailcentralpy-1.1.0.tar.gz 6.9 kB Without provenance Details for emailcentralpy-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for emailcentralpy 1.1.0
File Interpreter ABI Platform Actions
emailcentralpy-1.1.0-py3-none-any.whl Python 3 none any Details for emailcentralpy-1.1.0-py3-none-any.whl

Total release size: 14.4 kB

Release files / emailcentralpy-1.1.0.tar.gz

Download URL emailcentralpy-1.1.0.tar.gz
Size 6.9 kB
Tags Source
SHA-256 checksum
How to use checksums
c9a8d2defb3a4ea8b45b9952e7760c7e67e1c46c3dc6b6a62ef89bfa6dabb8a0
BLAKE2b-256 checksum
How to use checksums
3e666e848b498cbf1a191680ee3129721e44a8d4f8a67171c01c83cd5a43ac3d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.10

Release files / emailcentralpy-1.1.0-py3-none-any.whl

Download URL emailcentralpy-1.1.0-py3-none-any.whl
Size 7.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
04ba1f342941d51c35191aacae54726cf4750319c0363573121d3168a0dbc009
BLAKE2b-256 checksum
How to use checksums
906ae28a3069409f766866be6226b1bec8acb53a5c2c909ce2b38e44f65143f2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.10

Security for emailcentralpy 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page