Writing a provider

A provider is one TOML file, and writing one takes no Go and no build step. You copy the request example from the vendor's API docs, replace the values with img-cli's fields, and say where the answer is in the response. This page walks through a real one, Grsai, from an empty file to a working plugin. Every key is listed in the Provider file reference.

Before you start

From the vendor's docs, you need:

  • the base URL and how the API key is sent;
  • the request that starts a generation, with an example body;
  • for an asynchronous API, the request that checks a task, and its response;
  • the model ids and the options each one accepts.

Grsai's image API, from its docs:

POST https://grsaiapi.com/v1/api/generate
Authorization: Bearer sk-…
{"model": "nano-banana-2.1", "prompt": "…", "images": ["https://…"], "aspectRatio": "1:1", "imageSize": "1K", "replyType": "async"}
→ {"id": "6-f671fc51-…", "status": "running"}

GET https://grsaiapi.com/v1/api/result?id=6-f671fc51-…
→ {"id": "…", "status": "succeeded", "results": [{"url": "https://…png"}], "usage": {"credits": 1200}}
   status is running, succeeded, failed, or violation

1. Name the provider

Create grsai.toml:

schema = 1
name = "grsai"
title = "Grsai"
base_url = "https://grsaiapi.com"
currency = "credits"

[auth]
type = "bearer"
key_url = "https://grsai.ai/zh/dashboard/api-keys"

name is what users type in -m grsai:… and in provider login grsai. key_url is shown when img-cli asks for the key.

2. Write the request

An [apis.<name>] table describes one request shape. Paste the example body and replace each value with a template field:

[apis.generate]
submit = "POST /v1/api/generate"
body = '''
{
  "model": {{json .Model}},
  "prompt": {{json .Prompt}},
  {{with .Refs}}"images": [{{range .}}{{json (or .URL .Base64)}},{{end}}],{{end}}
  "aspectRatio": {{json .Aspect}},
  "imageSize": {{json .Size}},
  "replyType": "async",
}
'''

Three rules make this work:

  • Write every value with json. {{json .Prompt}} quotes and escapes the prompt, so quotes and newlines in it cannot break the body. Numbers stay numbers.
  • Unset flags disappear. An empty value renders as null, and img-cli drops every null field before sending. If the user did not pass --size, there is no imageSize in the request.
  • Trailing commas are fine. The body is read as HuJSON, so a range loop can end every item with a comma, and you can add // comments.

The templates are Go text/template. {{with .Refs}}…{{end}} writes the images field only when there are reference images; {{range .}} repeats for each one. (or .URL .Base64) sends a URL as is and a local file as base64, because Grsai accepts both.

3. Check it with --dry-run

You do not need a key yet. Add a placeholder model (step 5 finishes it) and preview the request:

[[models]]
id = "nano-banana-2.1"
kind = "image"
api = "generate"
img-cli --provider-file grsai.toml image -m grsai:nano-banana-2.1 'a "red" fox' --aspect 16:9 --ref https://example.com/a.png --dry-run
{
  "body": {
    "aspectRatio": "16:9",
    "images": ["https://example.com/a.png"],
    "model": "nano-banana-2.1",
    "prompt": "a \"red\" fox",
    "replyType": "async"
  },
  "dry_run": true,
  "method": "POST",
  "url": "https://grsaiapi.com/v1/api/generate"
}

Compare it with the vendor's example. If the template renders invalid JSON, the error shows the rendered text with line numbers.

4. Read the result

Tell img-cli how to poll and where things are in the responses:

[apis.generate]
# … submit and body as above
poll = "GET /v1/api/result?id={{urlquery .RawID}}"

[apis.generate.result]
id = "id"                      # in the submit response
status = "status"
done = ["succeeded"]
failed = ["failed", "violation"]
outputs = "results[].url"
error = "error"
cost = "usage.credits"

Paths use dots, and [] steps into each element of an array: results[].url collects every URL. .ID in a poll path is already path-escaped; in a query string use {{urlquery .RawID}}.

Status values listed in neither done nor failed, such as running, mean the task is still going. An API without poll is synchronous: the submit response already holds the result.

5. Declare the models

Each model says which flags it accepts. img-cli rejects anything else before sending a request:

[[models]]
id = "nano-banana-2.1"
kind = "image"
api = "generate"
supports = { aspect = ["auto", "1:1", "16:9", "9:16", "4:3", "3:4", "21:9"], size = ["1K", "2K", "4K"], ref = 14 }
Value Means
true any value
["a", "b"] one of these, compared case-insensitively; the listed spelling is sent
"4..15" an integer in this range
14 at most this many files

Leave a flag out of supports and it is refused: --n 2 now fails with grsai:nano-banana-2.1 does not support --n (it supports --aspect, --ref, --size). A model with no supports at all sends flags unchecked, with a warning.

6. Try it for real

img-cli provider add ./grsai.toml
img-cli provider login grsai
img-cli image -m grsai:nano-banana-2.1 "a red fox in the snow" --aspect 16:9 -o out/ -v

-v logs every HTTP request and response status. Check that the task finishes, the file is saved, and the cost is shown. Then try a failure, such as a prompt the vendor refuses, and check that the error message comes through.

That is the whole plugin. The finished file is providers/grsai.toml.

Common situations

The vendor implements a standard API

Point the model at a built-in protocol and skip the template:

[[models]]
id = "gemini-nano-banana-2.1"
kind = "image"
api = "gemini"

Built in: openai-images, openai-videos, openai-speech, and gemini. One file can mix them with its own APIs; see providers/laozhang.toml. The reference lists the flags each protocol can send.

Inputs go in an array with roles

Some APIs want every input as an item in one list, like MiniMax H3's content. Write one item per input, with with for optional ones and range for repeated ones:

body = '''
{
  "model": {{json .Model}},
  "content": [
    {"type": "text", "text": {{json .Prompt}}},
    {{with .FirstFrame}}{"type": "image_url", "role": "first_frame", "image_url": {"url": {{json .URL}}}},{{end}}
    {{range .Refs}}{"type": "image_url", "role": "reference_image", "image_url": {"url": {{json .URL}}}},{{end}}
  ],
  "resolution": {{json (upper .Resolution)}},
}
'''
media = "url"

upper turns img-cli's 768p into the 768P this API wants. media = "url" tells img-cli the API only fetches public URLs, so a local file is refused with a clear message instead of a vendor error.

The models come from the vendor's API

List them with [catalog] instead of [[models]], and translate each model's parameter schema into supports with [catalog.detail]. The VibeToken plugin does both; see providers/vibetoken.toml and Catalog. Models in [[models]] override the catalog, so you can fix one model by hand.

Models of one API name a field differently

When most models call it aspect_ratio but some call it ratio, give those models a fields entry and write the key with default:

body = '''{ {{json (default "aspect_ratio" .Fields.aspect)}}: {{json .Aspect}} }'''

[[models]]
id = "seedance-2"
fields = { aspect = "ratio" }
# …

One vendor, several regions

[hosts]
cn = "https://grsai.dakka.com.cn"

Users switch with img-cli provider host grsai cn.

Two services on the same backend

Inherit everything and change what differs:

schema = 1
extends = "vibetoken"
name = "routerbase"
base_url = "https://routerbase.com"
currency = "USD"

Retries and double charges

If the API deduplicates submits with a header, declare it, and img-cli sends a fresh key with each submit and retries it safely after network and server errors:

idempotency = "Idempotency-Key"

Without it, img-cli never retries a submit, because the vendor may already have started, and charged for, the first one.

What a provider cannot do

  • Send flags that are not part of img-cli's command line. Vendor-only options are left out on purpose, so the CLI stays the same everywhere.
  • Sign requests, as with AK/SK or JWT authentication. Only bearer tokens, a custom header, or a query parameter are supported.
  • Branch on complex logic. Templates have if, with, and range, but if a vendor needs much more, it is likely a candidate for a built-in protocol; open an issue.

Share it

The easiest way to share a provider is a GitHub repository with the file at its root, named img-cli.toml. Anyone can then install it by the repository's name:

img-cli provider add you/img-cli-acme
img-cli provider add you/img-cli-acme@v1.0.0   # a tagged version

A repository can hold several providers in other paths: you/providers/acme.toml. Tag releases so users can pin a version, and provider update brings them the newest one.

Fill in description, homepage, and author at the top of the file. add shows them, with the hosts that will receive the user's key, before it installs.

Add the GitHub topic img-cli-provider to the repository so others can find it. A file at any other URL works too: img-cli provider add https://…/acme.toml. To add it to the official list, open a pull request that adds it to providers/.

Before sharing, check:

  • [ ] --dry-run output matches the vendor's documented request for each API;
  • [ ] a real generation succeeds and saves its file, for each kind of model;
  • [ ] a failed task shows the vendor's error message;
  • [ ] every model has supports, and unsupported flags are refused;
  • [ ] key_url points to where users create a key.