---
name: interweb-ai
description: Use the Interweb AI API and its MCP server to list AI models and prices, ask other text models or the user's agents, generate images, videos and speech, follow generations until they finish, and check the AI credit balance or Coding Plan points. Use when the user asks to generate or edit an image, video, voice or audio file, to call or compare another AI model, to check AI credits, or to write code that calls the Interweb AI API at https://api.interweb.ao.
---

# Interweb AI API and MCP

This skill lets you use the Interweb AI API: hundreds of text, image, video and audio models, paid with the user's AI credits (or Coding Plan points for the text models in the plan). Answer the user in the language they write in.

## Setup

- API key: always read it from the environment variable `INTERWEB_API_KEY`. Keys start with `prod_` (real, spends credits) or `sandbox_` (test, free, fake results).
- REST base address: `https://api.interweb.ao/v1`
- MCP server: `https://api.interweb.ao/mcp` (Streamable HTTP, key in `Authorization: Bearer`). The suggested server name is `interweb-ai`.
- Check that the key is set without printing it: `test -n "$INTERWEB_API_KEY" && echo set || echo missing`.
- If the key is missing, ask the user to create one in the API Center (https://www.meu.interweb.ao/api-center.php?language=english&currency=2) with the `ai:read` and `ai:write` permissions and to run `export INTERWEB_API_KEY="prod_..."`. Credits are bought in All in One AI (https://www.meu.interweb.ao/all-in-one-ai.php?language=english&currency=2).

Security rules:

- Never print, echo, log or commit the key. Never write it into project files, code, `.env` files that are tracked by git, or chat messages. In code, read it from the environment.
- Never put the key in code that runs in a browser or a mobile app: calls to the API must come from a server.

## Choose the path: MCP tools first

If the MCP server is connected (in Claude Code the tools are named like `mcp__interweb-ai__generate_image`), use its tools:

| Tool | Use it to | Arguments |
|---|---|---|
| `list_models` | Find model ids and prices in credits | `type` (text, image, video, audio, agent), `q`, `limit` (all optional) |
| `get_model` | See the input fields of an image, video or audio model | `id` |
| `generate_image` | Create an image | `prompt`, optional `model`, `options` |
| `generate_video` | Create a video from text or from an image | `prompt`, optional `model`, `image_url`, `options` |
| `generate_audio` | Create speech or sound from text | `text` or `prompt`, optional `model`, `options` |
| `get_generation` | Check a generation that was still running | `id` |
| `chat` | Ask another text model or one of the user's agents | `model`, `messages` or `prompt`, optional `system`, `max_tokens` |
| `get_balance` | Check AI credits and Coding Plan points | none |

- Each `generate_*` waits up to about 50 seconds. If the result is ready, it returns our file links (and, for small images, the image itself). If not, it returns an `id`: call `get_generation` later (about every 10 seconds for images and audio, every 15 to 30 seconds for videos) and tell the user it is still running.
- Errors come back as a tool result with `isError: true` and a clear message. Read the message, explain it to the user and do not retry blindly.

If the MCP tools are not available, use the REST API below with `curl` (or the user's language of choice). To add the MCP server to Claude Code, the user can run:

```bash
claude mcp add --transport http --scope user interweb-ai https://api.interweb.ao/mcp --header "Authorization: Bearer $INTERWEB_API_KEY"
```

## REST API

All requests send `Authorization: Bearer $INTERWEB_API_KEY`. Model ids always contain a slash, for example `deepseek-ai/deepseek-v4-flash`. Never invent a model id: look it up first.

### Models and prices

```bash
curl -s "https://api.interweb.ao/v1/models?type=image&q=flux" -H "Authorization: Bearer $INTERWEB_API_KEY"
curl -s "https://api.interweb.ao/v1/models/MODEL_ID" -H "Authorization: Bearer $INTERWEB_API_KEY"
```

- `type` is `text`, `image`, `video`, `audio` or `agent`; `q` searches by name.
- Text models: `pricing` has `input`, `output` and `cached_input` in credits per 1M tokens, plus `context_length` and `max_output_tokens`.
- Media models: `pricing` has `unit` (`image`, `second` or `generation`) and `credits` per unit. `GET /v1/models/{id}` adds `input`: the fields the model accepts (name, type, required, options, default, min, max).

### Chat (OpenAI compatible)

```bash
curl -s "https://api.interweb.ao/v1/chat/completions" \
  -H "Authorization: Bearer $INTERWEB_API_KEY" -H "Content-Type: application/json" \
  -d '{"model": "deepseek-ai/deepseek-v4-flash", "messages": [{"role": "user", "content": "Hello"}], "max_tokens": 300}'
```

- Any OpenAI SDK works with `base_url="https://api.interweb.ao/v1"` and the key from `INTERWEB_API_KEY`.
- `model` can also be `agent/<id>` to use one of the user's agents (see `GET /v1/models?type=agent`).
- Always set `max_tokens`: credits for the input and for `max_tokens` of output are reserved before the call, and the unused part is returned. With `stream: true`, also send `"stream_options": {"include_usage": true}`.
- The API also speaks the Anthropic Messages format (`POST /v1/messages`) and the OpenAI Responses format (`POST /v1/responses`).

### Images, videos and audio

1. Send the job and keep the `id`:

```bash
curl -s "https://api.interweb.ao/v1/media/generations" \
  -H "Authorization: Bearer $INTERWEB_API_KEY" -H "Content-Type: application/json" \
  -H "Idempotency-Key: $(uuidgen)" \
  -d '{"model": "MODEL_ID", "input": {"prompt": "A bright, modern office with plants and soft morning light"}}'
```

The answer is `202` with `data.id`, `data.status` (`processing`) and `data.credits_reserved`. Put only fields from the model's `input` list inside `input`. Files such as a start image go as public URLs.

2. Poll every 5 to 10 seconds until `status` is no longer `processing`:

```bash
curl -s "https://api.interweb.ao/v1/media/generations/GENERATION_ID" -H "Authorization: Bearer $INTERWEB_API_KEY"
```

- `completed`: the files are in `data.outputs` and the real cost in `data.credits_charged`.
- `failed`: the reason is in `data.error`; nothing is charged.
- The output links are not permanent: download the files into the project (for example with `curl -fsSL -o public/img/hero.png URL`) and tell the user where they are.

### Balance

```bash
curl -s "https://api.interweb.ao/v1/ai/balance" -H "Authorization: Bearer $INTERWEB_API_KEY"
```

`data.credits` is the AI credit balance. With a Coding Plan, `data.subscription` shows the points left this week and this month.

## Spending rules

- Listing models, reading a model, checking a generation and checking the balance are free. Generations and chat spend credits.
- Before a video, a batch of images or any job likely to cost more than a few credits, look up the price, estimate the total (for example 5 seconds x credits per second), check the balance and ask the user to confirm.
- Generate one result first and show it before making variations. Never generate in a loop without the user asking for it.
- A generation keeps running and is charged even if you stop waiting, so do not send the same job twice: reuse the `id`, and send an `Idempotency-Key` with REST requests.
- Respect the rate limit: on `429`, wait for the `Retry-After` seconds.

## Sandbox

With a `sandbox_` key nothing is charged and no model runs: chat answers `Resposta de teste (sandbox).`, a generation is `completed` at once with a sample link and `0` credits, and the model list, prices and balance are real. If the user expects real results, tell them to switch to a `prod_` key.

## Errors

REST errors look like `{"error": {"code", "message", "correlation_id", "details"}}`.

| HTTP | code | Meaning and what to do |
|---|---|---|
| 401 | `unauthorized` | Key missing or wrong. Ask the user to check `INTERWEB_API_KEY`. |
| 402 | `insufficient_credits` | Not enough credits for the reservation (`details.required`, `details.balance`). Suggest buying credits, a cheaper model or a lower `max_tokens`. |
| 403 | `insufficient_scope` | The key lacks `ai:read` or `ai:write`. |
| 403 | `account_*` | The All in One AI account is not active. Ask the user to open All in One AI. |
| 404 | `model_not_found` | Wrong model id or wrong type. Look it up with `list_models` or `GET /v1/models`. |
| 409 | `agent_paused` | The agent is paused in All in One AI. |
| 422 | `validation_failed` | A field is missing or out of range. Check `details` and the model `input` fields. |
| 429 | `rate_limited` | Too many requests. Wait for `Retry-After`. |
| 429 | `plan_limit_reached` | Coding Plan points ran out and credits cannot be used. Wait for `details.resets_at`. |
| 502 | `upstream_error` | The AI service failed. Try again later or pick another model. |

## More

- MCP guide: https://docs.interweb.ao/en/mcp.md
- AI API guide: https://docs.interweb.ao/en/ai.md
- Full API reference: https://docs.interweb.ao/openapi.json
