Server Keys and Tokens
Pay for Polotno AI calls with a server key, and mint short-lived tokens with POST /api/tokens so browsers and apps can call the API without it
Paid calls spend your team's prepaid balance, so each one carries a credential that can pay: a server key, or a token minted with one. Send it in the Authorization header:
Authorization: Bearer <server key or token>The paid calls are the endpoints under /api/ai/ that the Polotno AI API lists. /api/ai/text and /api/ai/models are free. Other endpoints, such as the Cloud Render API, ignore the header and keep using ?KEY=.
Browser keys and server keys
| Browser key | Server key | |
|---|---|---|
| Key type in My account | Browser / Public | Server / Private |
| Domains | The sites where your editor runs | server, and nothing else |
| Where it lives | Your page's JavaScript | Your server's secrets |
| Can pay | No: 401 token_required | Yes, from anywhere |
A browser key licenses the editor and calls the free endpoints. It sits in your page's code, so anyone can copy it, and its domain list can't stop them: a script on a server can send any Origin header. If a browser key could pay, anyone who copied it could spend your balance. The API refuses it on paid calls with 401 token_required, on localhost too, so development works the way production does.
A server key pays from anywhere. Keep it on your server, and give browsers, desktop apps and phones a token instead.
A server key also works in a web page, where anyone who opens the page can copy it and spend your balance. If you accept that, use a separate server key there, so you can delete it at any time.
Create a server key
- Sign in to My account and open API & Usage.
- Click Create new API key. With no key yet, create your browser key first: the button appears after it.
- Choose Server / Private, then click Create server key.
The key shows a Server / Private badge, and its one domain is server. Store it as a secret on your server, such as an environment variable named POLOTNO_SERVER_KEY.
All server keys on your Polotno account spend one balance, which the whole team shares. The account owner adds funds on the Balance tab.
Call with a server key
Send the key in the header from your server:
curl https://api.polotno.com/api/ai/image/generate \
-H "Authorization: Bearer $POLOTNO_SERVER_KEY" \
--form-string "prompt=A lighthouse at dusk, watercolor" \
-o answer.json?KEY= in the URL takes a server key too. The header keeps the key out of URLs and server logs.
Mint a token
A token lets a client make paid calls without the server key. Your server mints it with POST /api/tokens, the client sends it with each call, and the API bills each call to the server key that minted it.
Request
POST https://api.polotno.com/api/tokens
Authorization: Bearer <server key>
Content-Type: application/jsonThe server key goes in the header. The endpoint ignores ?KEY=.
The JSON body is optional, and so is each field:
- expires_in: integer: the token's lifetime in seconds, from
60to3600(default:900, 15 minutes). - user: string: your ID for the end user, such as their ID in your database. Polotno records it with each charge the token makes. It holds 1 to 128 printable characters: letters, digits, punctuation, symbols and the space. The API refuses tabs, line breaks, invisible characters and
@, so the field can't hold an email address.
A body that isn't a JSON object, a body over 4,096 bytes, any other field, or a value outside these rules gets 400 invalid_request.
Answer
{
"access_token": "pt_…",
"token_type": "Bearer",
"expires_in": 900,
"expires_at": "2026-12-02T18:30:00Z"
}- access_token: string: the token. It starts with
pt_; the rest is opaque and varies in length, so store and send it as it came. - token_type: string:
Bearer. - expires_in: integer: the lifetime in seconds.
- expires_at: string: the expiry time in UTC, ISO 8601, to the second.
The fields follow OAuth 2.0's token response (RFC 6749, section 5.1), so OAuth client libraries read them. The answer carries Cache-Control: no-store.
The API seals any well-formed key into a token and checks the key at the token's first paid call, where a token minted with a browser key or a deleted key gets 401 invalid_token. Minting costs nothing: mint as often as you need, such as once per editor session.
Example
curl https://api.polotno.com/api/tokens \
-H "Authorization: Bearer $POLOTNO_SERVER_KEY" \
-H "Content-Type: application/json" \
-d '{"expires_in": 600, "user": "user-1842"}'Your server needs a route that mints a token for the signed-in user and forwards the API's response unchanged. Guard it with your own sign-in: anyone who can call it can spend your balance. Add a token endpoint has the route, at /api/polotno-token, for Express, Next.js, Cloudflare Workers and FastAPI. In the editor, setTokenFunc calls it.
Use a token
Send the token in the Authorization header of each paid call:
curl https://api.polotno.com/api/ai/image/generate \
-H "Authorization: Bearer $TOKEN" \
--form-string "prompt=A lighthouse at dusk, watercolor" \
-o answer.json- The call runs as the server key that minted the token, and that key's account pays.
- A token works from a browser, Electron, a mobile app or a server. Paid calls don't match the caller's
Originagainst the key's domains. - The API reads tokens from the header. A token in the URL doesn't work.
?KEY=can stay on the URL, as the editor sends it. With a token in the header, the API bills the token's server key.- A token that fails gets a 401. The API never falls back to
?KEY=. - Anyone who holds a token can spend your balance until it expires. Keep tokens out of logs and URLs.
Lifetime and refresh
A token works until its expires_at, unless you delete its server key first.
There is no refresh token. Mint a new token before the old one expires; the editor asks for one 30 seconds ahead. On 401 token_expired or invalid_token, mint a new token and retry the call once. The editor does that retry for you.
Revoke tokens and rotate a server key
To revoke a server key's tokens, delete the key: Remove server key on the API & Usage tab in My account. Each token it minted gets 401 invalid_token at its next paid call, because the API looks the key up on every paid call. You can't revoke one token alone. A short expires_in limits how long a leaked token works.
To replace a server key without downtime:
- Create a new server key.
- Switch your server to it, so new tokens come from the new key.
- Delete the old key. Its tokens get 401
invalid_token, and a client that mints a new token and retries carries on.
Errors
A refusal is JSON with a stable code, and each 401 carries a WWW-Authenticate header:
{ "error": { "code": "token_expired", "message": "The token has expired." } }| Status | code | Where | Cause | What to do |
|---|---|---|---|---|
| 401 | server_key_required | POST /api/tokens | No Authorization: Bearer header, a value that can't be a key, or a token in place of the key | Send a server key in the header |
| 400 | invalid_request | POST /api/tokens | The body breaks the rules in Request | Send expires_in and user alone, within their ranges |
| 401 | token_required | Paid calls | A browser key, in the header or in ?KEY= | Send a token or a server key |
| 401 | invalid_token | Paid calls | The token is malformed or altered, the Authorization header doesn't parse, or the token's key can't pay: unknown, deleted, a browser key, or not linked to a billing account | Mint a new token with a server key, then retry once |
| 401 | token_expired | Paid calls | The token is past its expires_at | Mint a new token, then retry |
invalid_token and token_expired send WWW-Authenticate: Bearer error="invalid_token" (RFC 6750); the other 401s send WWW-Authenticate: Bearer. Branch on code: the message is for people and can change.
The refusals about the balance and the account (402 insufficient_balance, 403 no_billing_account, 403 account_frozen and 503 billing_unavailable) are in the Polotno AI API errors.
Related
- Polotno AI API: the paid endpoints, their fields, limits and errors.
- Polotno AI: the editor's AI tools on a token, with
setTokenFuncandsetAiTools. - AI Design Generation: a demo that gets its tokens from polotno.com.