# REST API

Get started with the Coder API:

## Set up your environment

The code samples on each endpoint's page read your deployment URL and a session token from two environment variables, `CODER_URL` and `CODER_SESSION_TOKEN`, the same ones the Coder CLI reads.

If you are logged in with the Coder CLI v2.31.1 or later and have `jq` installed, set both from your login:

```sh
export CODER_URL="$(coder whoami -o json | jq -r '.[0].url')"
export CODER_SESSION_TOKEN="$(coder login token)"
```

For scripts and CI, create a long-lived token instead, with `coder tokens create` or under **Settings** > **Tokens** in the dashboard:

```sh
coder tokens create --name ci --lifetime 30d
```

Store the token in your secret store, and set the variables from it where the script runs:

```sh
export CODER_URL="https://coder.example.com"
export CODER_SESSION_TOKEN="$(cat /run/secrets/coder-token)"
```

Keep the token out of your shell history and out of committed files.

## Quickstart

Generate a token on your Coder deployment by visiting:

````txt
https://coder.example.com/settings/tokens
````

List your workspaces

````sh
# CLI
curl https://coder.example.com/api/v2/workspaces?q=owner:me \
-H "Coder-Session-Token: <your-token>"
````

## Use cases

See some common [use cases](/docs/v2.39/reference/#use-cases) for the REST API.

## Request size limits

An endpoint that accepts a request body reads at most 4 MiB of it, unless it
sets a limit of its own.
An endpoint that sets its own limit declares `413` in this reference and
names the limit in the description, so the per-endpoint page is where to look
one up. A few endpoints answer `413` for a reason other than the size of
the request body, and their descriptions say so.

A body that exceeds the limit that applies to it is answered with
`413 Payload Too Large`, and the response names the limit it exceeded:

````json
{
  "message": "Request body too large.",
  "detail": "Maximum request body size is 4194304 bytes."
}
````

The limits are fixed. There is no deployment option that raises them.

## Sections

<children>
  This page is rendered on https://coder.com/docs/reference/api. Refer to the other documents in the `api/` directory.
</children>
