# HTTP API

Covers: the HTTP API's address and key, its resources and routes, an instance's routes, a Task's fields and the errors.

For: developers who call poema cloud from a language with no SDK, such as Node, Go or Rust, and agents that use curl.

The CLI, the Python SDK and `poema-mcp` are clients of this API: anything they do, a request here does too. An
instance is a `Task` in the API. The [Quickstart](https://client-test.poema.dev/docs.md)'s HTTP tab makes a first one.

## Address and key

Every route is under `$POEMA_URL/v1alpha1/namespaces/NAMESPACE/`, where `NAMESPACE` is your organization's namespace:
the word after `poema_ns_` in your key, `poema_ns_XXXXXXXX_...`, shown with your keys in Settings, Account. Send the key as
`X-API-Key` on every request. Bodies are JSON.

```
export POEMA_URL=https://api.client-test.poema.dev
export POEMA_API_KEY=poema_ns_...
```

```sh
export POEMA_NAMESPACE=YOUR_NAMESPACE
API="$POEMA_URL/v1alpha1/namespaces/$POEMA_NAMESPACE"
curl -fsS "$API/tasks" -H "X-API-Key: $POEMA_API_KEY"
```

## Resources

The kinds are `tasks`, `workspaces`, `gateways` and `models` ([Concepts](https://client-test.poema.dev/docs/concepts.md)):

- `PUT $API/KIND/NAME` creates or replaces one. Its body is the manifest that `poema apply` sends:
  `{"apiVersion": "poema.dev/v1alpha1", "kind": "Task", "metadata": {"name": "NAME"}, "spec": {...}}`.
- `GET $API/KIND` lists them, as `{"items": [...]}`, and `GET $API/KIND/NAME` reads one.
- `DELETE $API/KIND/NAME` deletes one. For an instance it answers at once with `202`; with `?wait=true` it answers
  once the instance is gone.

A list answers 100 at most; when `nextPageToken` is set, ask again with `?pageToken=` that value.

Secrets hold the keys a Model names, and are write-only:

- `PUT $API/secrets/NAME` with `{"data": {"api-key": "..."}}` creates or replaces one. Send the value from a file or a
  variable, never typed on a command line.
- `GET $API/secrets` lists their names and keys, never their values, and `DELETE $API/secrets/NAME` deletes one.

## An instance's routes

- `GET $API/tasks/NAME/logs` is its command's output: `?tail=N` its last N lines, `?follow=true` its lines as they come,
  for as long as it runs.
- `GET $API/tasks/NAME/events` is its recent activity, as `{"items": [...]}`.
- `GET $API/tasks/NAME/watch` streams the instance, a line of JSON at each change of its status.
- `POST $API/tasks/NAME/suspend` and `POST $API/tasks/NAME/resume`, with the body `{}`, suspend it and resume it. Both
  answer at once: read the instance until `status.phase` is `Suspended` or `Running`.
- `POST $API/tasks/NAME/timeout` with `{"timeoutSeconds": N}` suspends it N seconds from now, at most your plan's session
  length; 0 sets that length.
- `POST $API/tasks/NAME/fork` with `{"names": ["try-a", "try-b"]}` forks a suspended instance into one new instance a
  name, each on a copy of its `/workspace`; `"memory": true` copies its memory too, and `"suspend": true` keeps each
  fork suspended. An instance that is not suspended answers `409 Conflict`.

## Quota and usage

- `GET $API/quota` is your plan's limits next to what is in use.
- `GET $API/usage?from=YYYY-MM-DD&to=YYYY-MM-DD` is your organization's use between two days: model tokens and their
  cost, and compute in vCPU-hours, in total, by model and by day.

## A Task's fields

`metadata.name` names it and `metadata.labels` labels it. Its `spec`:

- `command`, the one field it needs: a list, such as `["sh", "-c", "echo hello"]`.
- `image`: poema's base image unless given ([Deploy from git](https://client-test.poema.dev/docs/git.md) lists the others).
- `env`: a list of `{"name": ..., "value": ...}`. Values are plain text that your keys read: a model's key goes in a Model.
- `workspaces`: a list of `{"name": ...}`, and `gateway` and `model`, each `{"name": ...}`. Without a gateway it has no
  network.
- `debug`: `true` for Access from your machine, which commands, files, terminals and SSH need.
- `resources`: `{"requests": {"cpu": "2", "memory": "2Gi"}}`; without it, 1 vCPU and 256 MiB.
- `timeoutSeconds`: it ends that many seconds after it starts, at most your plan's session length. `onTimeout` is
  `suspend`, the default, or `delete`.
- `onExit`: `suspend` or `delete` when its command exits. Without it, the instance stays up after its command ends.

Its `status.phase` is `Pending`, `Running`, `Suspended`, `Failed`, or `Terminating` while it is deleted.
Its `status.conditions` say why.

## Errors

Every error is JSON, `{"error": {"code": "Invalid", "message": "..."}}`, with the matching status: `Invalid` 400,
`Unauthorized` 401, `Forbidden` 403, `NotFound` 404, `Conflict` 409 and `Unavailable` 503, which a retry may cure.
Read the message: it says what to change.

Last checked: unreleased checkout.
