Polotno AI API
Call Polotno AI over HTTP, with the image and design endpoints, their fields and limits, what each call costs, and the error codes
Polotno AI is Polotno's paid API for AI images and designs. The editor's AI tools run on it (Polotno AI shows the setup), and you can call it over HTTP from your server or from any client with a token. Each call spends your team's prepaid balance.
Before you start
Each call needs a server key, or a token minted with one. A browser key can't pay. Server Keys and Tokens shows how to create the key and mint tokens.
Your Polotno account has one balance, which the whole team shares. The account owner adds funds on the Balance tab in My account. GET /api/pricing lists the price of each model.
Quick start
These steps use curl and jq. Mint a token with your server key, kept here in POLOTNO_SERVER_KEY:
TOKEN=$(curl -s https://api.polotno.com/api/tokens \
-H "Authorization: Bearer $POLOTNO_SERVER_KEY" \
-H "Content-Type: application/json" \
-d '{"expires_in": 600}' | jq -r .access_token)Generate an image with the token:
curl -s https://api.polotno.com/api/ai/image/generate \
-H "Authorization: Bearer $TOKEN" \
--form-string "prompt=A red bicycle against a yellow wall, morning light" \
--form-string "aspect_ratio=16:9" \
-D headers.txt -o answer.json -w "%{http_code}\n"The call can take a few minutes. It prints 200 when the image is in answer.json; any other status leaves the error there (Errors). headers.txt holds the call's X-Run-Id. Save the image:
jq -r '.src | split(",")[0]' answer.json # the format, such as data:image/jpeg;base64
jq -r '.src | split(",")[1]' answer.json | base64 --decode > bicycle.jpg--form-string sends a text field as it is. With -F, a value that starts with @ or < names a file.
Endpoints
Paths are on https://api.polotno.com. A product's id is its path after /api/, with . for /.
| Endpoint | Product id | What it does | In the editor |
|---|---|---|---|
POST /api/ai/image/generate | ai.image.generate | Makes an image from a prompt | Generate, in the AI tab |
POST /api/ai/image/edit | ai.image.edit | Changes an image as a prompt describes | |
POST /api/ai/image/expand | ai.image.expand | Extends an image past its edges | Expand |
POST /api/ai/image/upscale | ai.image.upscale | Enlarges an image 2× or 4× | Upscale |
POST /api/ai/image/remove-object | ai.image.remove-object | Erases a masked area and fills it in from its surroundings | Generative fill, Remove |
POST /api/ai/image/replace-object | ai.image.replace-object | Paints a masked area from a prompt | Generative fill, Replace |
POST /api/ai/design/create | ai.design.create | Makes a one-page design from a prompt | |
POST /api/ai/design/edit | ai.design.edit | Restyles a design from a prompt |
Two free endpoints need no key: GET /api/ai/models lists each tool's models, and GET /api/pricing adds their prices (Models and prices).
Requests
Credentials
Send Authorization: Bearer <server key or token>. A server key also works as ?KEY= in the URL. Browser code can call these endpoints with a token: the API allows any origin and the Authorization header. Server Keys and Tokens covers both credentials and their refusals.
Body
The image tools take multipart/form-data, at most 40 MB (41,943,040 bytes) per request, with a Content-Length header: curl and browsers set it for a form. The AI design endpoints take a JSON body.
The model field
Each image tool takes an optional model field: one of the tool's model ids, listed under each tool below and in GET /api/ai/models. Without it, or with an empty value, the call runs the tool's default model. An id the tool doesn't have gets 400. The AI design endpoints take no model and run their one model.
Prompt length
A prompt holds at most 2,000 characters. A line break counts as one character, as \n or as \r\n, which browsers send in forms. The API counts UTF-16 code units, as JavaScript's String.length does, so an emoji can count as two. A longer prompt gets 400.
Timing
One call makes one image. An image call gives the model a little over five minutes, then answers 504. AI design calls take minutes too.
Set your HTTP client's timeout to 10 minutes. Node's built-in fetch stops waiting for response headers after 300 seconds by default (undici's headersTimeout). If your client gives up after the model has started, the API charges the call and you get no result (When the API charges a call).
Answer
Each image tool answers 200 with JSON:
{ "type": "image", "src": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQ…" }- type:
image. - src: the result as a
data:URL. Accept anhttps:URL here too. The MIME type at its start names the format.
A result can be several MB. Decode src and keep the file in your own storage.
Edit, Replace object and Upscale answer PNG when the input PNG has an alpha channel or a palette, and JPEG otherwise. A JPEG result is a fraction of a PNG's size, so drop the alpha channel of an image with no transparent pixels before you send it.
X-Run-Id
Every answer from a paid endpoint carries an X-Run-Id header with the call's UUID, refusals and errors included:
X-Run-Id: 6f1d3c2a-9b8e-4f7a-a1c5-3e2d7b9f0c44The API lists it in Access-Control-Expose-Headers, so browser code can read it. Log it, and quote it when you ask Polotno about a call.
Image tools
Fields are form fields: text, or a file where the list says so. The editor sends each image at most 4096 px on its long side.
Generate
POST /api/ai/image/generate makes an image from a prompt.
- prompt: text (required): the image to make.
- aspect_ratio: text: one of the model's ratios in the table below (default:
1:1). - reference: file: an image for the model to follow. Repeat the field to send several, up to the model's limit.
- model: text: a model id from the table (default:
seedream-lite).
model | Name | reference files | aspect_ratio values |
|---|---|---|---|
seedream-lite | Seedream 5 Lite | Up to 14 | 1:1, 4:3, 3:4, 3:2, 2:3, 16:9, 9:16, 21:9 |
ideogram-turbo | Ideogram V4 Turbo | None | 1:1, 4:3, 3:4, 3:2, 2:3, 16:9, 9:16, 5:4, 4:5, 2:1, 1:2 |
flux | FLUX 3 | Up to 10 | 1:1, 4:3, 3:4, 3:2, 2:3, 16:9, 9:16, 5:4, 4:5, 2:1, 1:2, 7:5, 5:7, 21:9, 9:21 |
gpt-image | GPT Image 2.6 | Up to 4 | 1:1, 4:3, 3:4, 3:2, 2:3, 16:9, 9:16 |
nano-banana-pro | Nano Banana Pro | Up to 14 | 1:1, 4:3, 3:4, 3:2, 2:3, 16:9, 9:16, 5:4, 4:5, 21:9 |
The API passes reference files to the model unchanged and doesn't check their format. The editor sends each one as a PNG, at most 2048 px on its long side.
curl -s https://api.polotno.com/api/ai/image/generate \
-H "Authorization: Bearer $TOKEN" \
--form-string "prompt=This sneaker on a beach at sunset, product photo" \
--form-string "model=flux" \
--form-string "aspect_ratio=3:4" \
-F "reference=@sneaker.png" \
-o answer.jsonEdit
POST /api/ai/image/edit changes an image as the prompt describes, with optional reference images.
- image: PNG file (required): the image to change.
- prompt: text (required): the change, such as "turn it into a night scene".
- reference: file: more images for the model, up to its limit.
- model: text: a model id from the table (default:
nano-banana-pro).
model | Name | reference files |
|---|---|---|
nano-banana-pro | Nano Banana Pro | Up to 13 |
gpt-image | GPT Image 2.6 | Up to 3 |
nano-banana-lite | Nano Banana 2 Lite | Up to 13 |
flux-canny-pro | FLUX Canny Pro | None |
FLUX Canny Pro keeps the edges of image and redraws the rest from the prompt.
curl -s https://api.polotno.com/api/ai/image/edit \
-H "Authorization: Bearer $TOKEN" \
-F "image=@living-room.png" \
--form-string "prompt=Replace the sofa with the one in the reference photo" \
-F "reference=@sofa.png" \
-o answer.jsonExpand
POST /api/ai/image/expand extends an image past its edges, as Expand does in the editor.
- image: PNG file (required).
- frame: text (required): the new area as JSON, in fractions of the image's width and height.
{"x":-0.25,"y":0,"width":1.5,"height":1}adds a quarter of the width on each side. The frame holds the whole image (xandyat most 0,x + widthandy + heightat least 1) and reaches past it on at least one side. - prompt: text: what to paint in the new area.
- model: text:
bria-expand(Bria Expand), the tool's one model.
The result covers the whole frame, at most 5000 px on its longer side: the API scales a larger canvas down, image included, and keeps its aspect ratio.
curl -s https://api.polotno.com/api/ai/image/expand \
-H "Authorization: Bearer $TOKEN" \
-F "image=@photo.png" \
--form-string 'frame={"x":-0.25,"y":0,"width":1.5,"height":1}' \
-o answer.jsonUpscale
POST /api/ai/image/upscale enlarges an image, as Upscale does in the editor.
- image: PNG file (required).
- scale: text:
2or4(default:2). - model: text:
topaz-upscale(Topaz Upscale), the tool's one model.
The output holds at most 4096 × 4096 pixels in area: width × height × scale² must stay within 16,777,216, or the call gets 400. At scale 4, the largest square input is 1024 × 1024. The editor shrinks a larger image before it sends it.
curl -s https://api.polotno.com/api/ai/image/upscale \
-H "Authorization: Bearer $TOKEN" \
-F "image=@logo.png" \
--form-string "scale=4" \
-o answer.jsonRemove object
POST /api/ai/image/remove-object erases the masked area and fills it in from its surroundings, as Generative fill does in Remove mode.
- image: PNG file (required).
- mask: PNG file (required): the same pixel size as
image. White pixels mark the area to change, and black pixels stay as they are. - model: text:
bria-eraser(Bria Eraser), the tool's one model.
curl -s https://api.polotno.com/api/ai/image/remove-object \
-H "Authorization: Bearer $TOKEN" \
-F "image=@street.png" \
-F "mask=@mask.png" \
-o answer.jsonReplace object
POST /api/ai/image/replace-object paints the masked area from a prompt, as Generative fill does in Replace mode.
- image: PNG file (required).
- mask: PNG file (required): as for Remove object.
- prompt: text (required): what to paint, such as "a red vintage car".
- model: text:
flux-fill-pro(FLUX Fill Pro), the tool's one model.
curl -s https://api.polotno.com/api/ai/image/replace-object \
-H "Authorization: Bearer $TOKEN" \
-F "image=@street.png" \
-F "mask=@mask.png" \
--form-string "prompt=A red vintage car" \
-o answer.jsonExpand, Upscale, Remove object and Replace object take no reference files.
AI design
AI design is experimental. Its request and answer can change.
Create a design
POST /api/ai/design/create makes a one-page design from a prompt. JSON body:
- prompt: string (required): the design to make.
It answers { "design": { … } }, a Polotno JSON design with one page. Load it with store.loadJSON(answer.design). AI Design Generation runs it from a side panel.
curl -s https://api.polotno.com/api/ai/design/create \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"prompt": "A business card for a coffee shop, warm brown tones"}' \
-o design.jsonEdit a design
POST /api/ai/design/edit restyles a design from a prompt, such as its colors and fonts. JSON body, at most 1 MB (1,048,576 bytes):
- design: object (required): a Polotno JSON design with at least one page, as
store.toJSON()returns it. - prompt: string (required): the change, such as "a dark theme with neon accents".
The API drops default values, puts placeholders in place of media URLs, and sends the design to the model as indented JSON. That JSON must fit in 100,000 bytes, or the call gets 413.
It answers { "design": { … } }: your design with the model's changes. Pages match by position and elements by id. Every page and element of your design stays: the edit adds and removes none.
jq -n --slurpfile design my-design.json \
'{design: $design[0], prompt: "A dark theme with neon accents"}' \
| curl -s https://api.polotno.com/api/ai/design/edit \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
--data-binary @- \
-o edited.jsonModels and prices
GET /api/ai/models
Lists each AI product with its models, without prices. It needs no key, and browsers and proxies may cache it for 5 minutes (Cache-Control: public, max-age=300). The editor reads this list. An extract:
{
"products": [
{
"id": "ai.image.generate",
"name": "Generate an image",
"default_model": "seedream-lite",
"models": [
{
"id": "seedream-lite",
"name": "Seedream 5 Lite",
"references": 14,
"aspect_ratios": ["1:1", "4:3", "3:4", "3:2", "2:3", "16:9", "9:16", "21:9"]
}
]
},
{
"id": "ai.image.expand",
"name": "Expand",
"default_model": "bria-expand",
"models": [{ "id": "bria-expand", "name": "Bria Expand" }]
}
]
}- products[].id: the product id, as in Endpoints.
- products[].name: the product's display name.
- products[].default_model: the model a call runs without
model. - models[].id: a value for the
modelfield. - models[].name: a display name for a model picker.
- models[].references: the most
referencefiles the model takes,0for none. Set on Generate and Edit models. - models[].aspect_ratios: the
aspect_ratiovalues the model takes. Set on Generate models.
Polotno adds fields to this list and never removes or changes one. Build a model picker from it at runtime, so the picker follows model changes.
GET /api/pricing
The same products and models with their prices, for your backend and your own price list. It needs no key and has the same 5-minute cache. It adds four fields:
- currency:
USD. - updated: the day Polotno last checked the prices,
YYYY-MM-DD. - products[].unit: what one call makes:
imageordesign. - models[].price: the price of one call with the model, in
currency.
The live list has the prices.
When the API charges a call
A call costs its model's price in /api/pricing. The API charges the balance when:
- it answers 200 with the result, or
- your client disconnects after the model has started the job.
Everything else is free: refusals, input errors, failures of the model or its provider, errors on Polotno's side, and a disconnect before the model starts.
While a call runs, the API sets its price aside. A new call gets 402 insufficient_balance when the balance, minus the prices set aside for running calls, doesn't cover it.
Errors
Refusals
A refusal is JSON with a stable code:
{ "error": { "code": "insufficient_balance", "message": "The balance is too low for this call." } }| Status | code | Cause | What to do |
|---|---|---|---|
| 401 | token_required | The call carries a browser key | Send a token or a server key |
| 401 | invalid_token | The token is malformed or altered, or its key can't pay | Mint a new token, then retry once |
| 401 | token_expired | The token is past its expires_at | Mint a new token, then retry |
| 402 | insufficient_balance | The balance, minus the prices set aside for running calls, doesn't cover this call | The account owner adds funds on the Balance tab |
| 403 | no_billing_account | The key isn't linked to exactly one Polotno account | Contact us with the X-Run-Id |
| 403 | account_frozen | Polotno paused paid calls for the account | Contact us |
| 503 | billing_unavailable | Billing didn't answer within a few seconds, so the API didn't call the model | Retry |
| 410 | endpoint_retired | The path is retired | Call the path that message names |
A browser key gets token_required even when its account has no balance or is frozen. Polotno adds new codes and never changes what a code means. Branch on code: the message is for people and can change. Minting has two more codes, in Server Keys and Tokens.
Other answers
These answers keep a plain-text body, and the API charges for none of them:
| Status | Body | Cause |
|---|---|---|
| 400 | Key is invalid | The call has no key, or its key isn't a Polotno key |
| 400 | A message that names the field, such as "image" must be a PNG | An input error |
| 400 | Can not …, such as Can not generate image | The model failed on this input, such as a prompt it refuses or an image it can't read |
| 403 | A message naming what Polotno blocked | Polotno blocked the account or the calling domain |
| 404 | Unknown tool | No image tool has that name |
| 411 | Content-Length is required | The form came without a Content-Length header |
| 413 | Request is too large | A form over 40 MB, or an AI design edit body over 1 MB |
| 413 | Design is too large | The design to edit doesn't fit the model's 100,000 bytes |
| 502 | … is temporarily unavailable | The model or its provider failed; retry later |
| 504 | … is temporarily unavailable | The model didn't finish in time |
| 500 | An error on Polotno's side |
The AI design endpoints answer some failures as JSON with an error field instead, such as { "error": "AI design generation is temporarily unavailable, please retry" } with 502. In some regions the AI design models are unavailable: create answers 503 and edit answers 400, each with a message that the feature isn't available there.
Retired endpoints
These paths answer 410 endpoint_retired, and the message names the path to call:
| Path | Call instead |
|---|---|
/api/ai/image | /api/ai/image/generate |
/api/get-stable-diffusion | /api/ai/image/generate |
/api/ai/image-to-image | /api/ai/image/edit |
/api/ai/tools/expand | /api/ai/image/expand |
/api/ai/tools/upscale | /api/ai/image/upscale |
/api/ai/tools/edit | /api/ai/image/edit |
/api/ai/tools/remove | /api/ai/image/remove-object |
/api/ai/tools/replace | /api/ai/image/replace-object |
Their replacements take a token or a server key.
Related
- Server Keys and Tokens: the credentials that pay, and
POST /api/tokens. - Polotno AI: the editor's AI tab and canvas tools on this API.
- AI Image Tools: the canvas tools' requests, and handlers for your own provider.
- AI Design Generation: a side panel on
/api/ai/design/create. - Error Handling: the editor reports these refusals as
FETCH_FAILED, withapiCodeandrunId.