# Create and manage API keys

> Create, use, rotate and revoke pushq API keys, and choose between writer and admin roles, in the console, through the API, or in a self-hosted server's config.

Source: https://pushqueue.dev/docs/guides/api-keys/

Your apps authenticate to pushq with an API key sent as a bearer token. This page covers creating keys, what each role may do, keeping keys safe, rotating them without downtime and revoking one that leaked. Self-hosters can also define keys in the server config; see [Config-file keys](#config-file-keys-self-hosted).

## How keys work

- A key looks like `pq_` followed by 32 random characters (24 random bytes, base64url-encoded).
- The full key is returned **once**, when you create it. pushq stores only its SHA-256 hash, so nobody (including you) can read it back later. Lists show only the first 11 characters (the `prefix`, e.g. `pq_AbC12dEf`) so you can tell keys apart.
- Every key has a role, `writer` or `admin`. Pick the smallest one that does the job.
- On the hosted service, a key belongs to the team that created it and only sees that team's queues.
- Revoking a key takes effect on the next request that uses it.

## Key roles

| Action | Endpoints | Writer | Admin |
|---|---|---|---|
| Read a queue you name, its stats, its tasks and their attempts | `GET /v1/queues/{queue}`, `.../stats`, `.../tasks`, `.../tasks/{id}`, `.../attempts`, `GET /v1/version` | Yes | Yes |
| Create and change queues; pause, resume, purge | `POST /v1/queues`, `PUT`/`PATCH /v1/queues/{queue}`, `:pause`, `:resume`, `:purge` | Yes | Yes |
| Enqueue, force-run and delete tasks | `POST .../tasks`, `.../tasks:batch`, `.../tasks/{id}:run`, `DELETE .../tasks/{id}` | Yes | Yes |
| List queues | `GET /v1/queues` | No | Yes |
| Delete queues | `DELETE /v1/queues/{queue}` | No | Yes |
| Manage team members, invites, API keys and the team name | `/v1/users`, `/v1/invites`, `/v1/api-keys`, `PATCH /v1/teams/current` | No | Yes |

A **writer** key works only with queues it already knows by name: it can't list queues or delete them. That makes it the right choice for application code that enqueues tasks. Use an **admin** key for tooling that needs to see everything or manage the team, such as a provisioning script.

A few endpoints don't accept any API key, because they act on a person rather than a team: `PATCH /v1/me`, `POST /v1/me/password`, `GET /v1/teams`, `POST /v1/teams` and `POST /v1/teams/{id}:switch` need a signed-in dashboard session. See [Team members](https://pushqueue.dev/docs/guides/team-members/).

When a key isn't allowed to do something, the API answers `403` with code `permission_denied`:

```json
{
  "type": "about:blank",
  "title": "Forbidden",
  "status": 403,
  "code": "permission_denied",
  "detail": "the writer role is not allowed to do this",
  "instance": "/v1/queues"
}
```

A missing, unknown or revoked key gets `401` with code `unauthenticated` (`"sign in or send a valid API key"`).

## Create a key in the console

Only team admins can create keys.

1. Sign in to the [console](https://console.pushqueue.dev/) (on a self-hosted server, open the dashboard at `/`).
2. Open the account menu in the top-right corner and choose **Settings**, then the **API keys** tab.
3. Under **Create an API key**, enter what will use the key, for example `billing-service` (1–100 characters).
4. Pick **Writer** or **Admin** and click **Create key**.
5. Copy the key from the dialog and store it in your secret manager. Click **I've saved it** to close the dialog. It will not be shown again.

The table below the form lists every key with its name, prefix, role, when and by whom it was created, and when it was last used.

Use this when you set up a new app or environment and need a key for it.

## Manage keys through the API

The `/v1/api-keys` endpoints need an **admin** key or a signed-in admin user. On the hosted service they act on the team the key (or session) belongs to. The SDKs don't wrap these endpoints, so the examples use plain HTTP; call them with any HTTP client.

### Create a key

`POST /v1/api-keys`

| Parameter | Type | Default | Description |
|---|---|---|---|
| `name` | string | required | What uses the key, e.g. `billing-service`. 1–100 characters after trimming spaces. |
| `role` | string | required | `writer` or `admin`. |

```bash
curl -X POST https://api.pushqueue.dev/v1/api-keys \
  -H "Authorization: Bearer $PUSHQ_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"name": "billing-service", "role": "writer"}'
```

Response, `201 Created`:

```json
{
  "key": {
    "id": 42,
    "name": "billing-service",
    "prefix": "pq_AbC12dEf",
    "role": "writer",
    "created_at": "2026-11-28T10:15:00Z"
  },
  "secret": "pq_AbC12dEfGh3456789ijklMNOPqrstuvw"
}
```

| Field | Type | Description |
|---|---|---|
| `secret` | string | The API key itself. Shown only once; store it now. |
| `key` | object | The key's metadata (see [Key object](#key-object)). |

An empty or too-long name, or a role other than `writer`/`admin`, returns `400 invalid_argument`.

Use this when you provision environments with a script and want each service to get its own key.

### List keys

`GET /v1/api-keys`

```bash
curl https://api.pushqueue.dev/v1/api-keys \
  -H "Authorization: Bearer $PUSHQ_API_KEY"
```

```json
{
  "keys": [
    {
      "id": 42,
      "name": "billing-service",
      "prefix": "pq_AbC12dEf",
      "role": "writer",
      "created_by": "ada@example.com",
      "created_at": "2026-11-28T10:15:00Z",
      "last_used_at": "2026-11-28T11:02:00Z"
    }
  ],
  "config_keys": 0
}
```

| Field | Type | Description |
|---|---|---|
| `keys` | array | Keys created in the dashboard or through the API, newest first. On the hosted service, only the current team's keys. |
| `config_keys` | integer | How many more keys are defined in the server config file. Always `0` on the hosted service. |

#### Key object

| Field | Type | Description |
|---|---|---|
| `id` | integer | Use it to revoke the key. |
| `name` | string | The name given at creation. |
| `prefix` | string | The first 11 characters of the key, for recognizing it. |
| `role` | string | `writer` or `admin`. |
| `created_by` | string | Email of the user who created it. Omitted when it was created with an API key. |
| `created_at` | string (date-time) | When it was created. |
| `last_used_at` | string (date-time) | When it last authenticated a request. Omitted if never used. Updated at most once a minute per key, so treat it as approximate. |

### Revoke a key

`DELETE /v1/api-keys/{id}`

```bash
curl -X DELETE https://api.pushqueue.dev/v1/api-keys/42 \
  -H "Authorization: Bearer $PUSHQ_API_KEY"
```

Returns `204 No Content`. Requests using that key fail with `401` from then on. An `id` that doesn't exist, or belongs to another team, returns `404 not_found`. There is no undo; create a new key instead.

In the console, click **Revoke** next to the key and confirm.

## Use a key

Send the key in the `Authorization` header of every request:

```http
Authorization: Bearer pq_...
```

Every SDK takes the key when you create the client and sends the header for you. None of them read an environment variable on their own, so pass it in explicitly:


**TypeScript**

```ts

const pq = new Pushq({ apiKey: process.env.PUSHQ_API_KEY });
```

**Python**

```python
import os
from pushq import Pushq

pq = Pushq(api_key=os.environ["PUSHQ_API_KEY"])
```

**Go**

```go
// "" means the hosted API.
pq := pushq.New("", os.Getenv("PUSHQ_API_KEY"))
```

**Java**

```java
Pushq pq = Pushq.builder()
    .apiKey(System.getenv("PUSHQ_API_KEY"))
    .build();
```

**.NET**

```csharp
using var pq = new PushqClient(
    apiKey: Environment.GetEnvironmentVariable("PUSHQ_API_KEY"));
```

**curl**

```bash
curl https://api.pushqueue.dev/v1/queues/emails \
  -H "Authorization: Bearer $PUSHQ_API_KEY"
```

For a self-hosted server, also pass its URL. See [Install & configure an SDK](https://pushqueue.dev/docs/getting-started/sdks/) for every constructor option.

The `pushq` command-line client reads `PUSHQ_API_KEY` (or `--api-key`) and talks to `PUSHQ_SERVER` (or `--server`, default `http://localhost:8080`).

## Store keys safely

- Keep keys out of source control. Load them from an environment variable such as `PUSHQ_API_KEY`, set by your secret manager, deployment platform or a git-ignored `.env` file.
- Give each service or environment its own key, named after it. Then you can revoke one without touching the others, and `last_used_at` tells you which one is in use.
- Prefer writer keys in application code. Keep admin keys for people and tooling.
- Never ship a key to a browser or mobile app. Anyone who can read the page can read the key. Enqueue tasks from your backend instead.

## Rotate a key with zero downtime

pushq accepts any number of keys at once, so you can overlap old and new:

1. Create a new key with the same role and a name that marks it as the replacement, for example `billing-service-2026-11`.
2. Deploy the new key to the service (update the secret, then restart or redeploy).
3. Check that the old key's `last_used_at` stops moving. It updates at most once a minute, so wait a few minutes after the rollout finishes.
4. Revoke the old key.

Use this when a key is old, someone who knew it left, or your policy requires periodic rotation.

## Revoke a leaked key

If a key may have been exposed (committed to a repository, pasted in a ticket, printed in logs):

1. Revoke it immediately in **Settings → API keys** or with `DELETE /v1/api-keys/{id}`. It stops working on the next request.
2. Create a replacement and deploy it. Services using the old key get `401` until they have the new one, so do this quickly, or create and deploy the replacement first if you can afford a few minutes.
3. Review what the key could do. A writer key could enqueue, run and delete tasks and change or purge queues it knew by name; an admin key could also list and delete queues and manage members and keys. Check your queues and team in the console.

## Config-file keys (self-hosted)

A self-hosted server can also take keys from its configuration. These exist alongside keys created in the dashboard.

```yaml
# pushq.yaml
api_keys:
  - { key: "pq_admin_<generate>",  role: admin }
  - { key: "pq_writer_<generate>", role: writer }
```

Or as an environment variable, a comma-separated list of `role:key` pairs. When set, it replaces the file's `api_keys` list:

```bash
PUSHQ_API_KEYS="admin:pq_admin_<generate>,writer:pq_writer_<generate>"
```

The key can be any non-empty string, and the role must be `admin` or `writer` or the server refuses to start. Generate a long random value, for example:

```bash
echo "pq_$(openssl rand -hex 24)"
```

How they differ from dashboard keys:

| | Config-file keys | Dashboard keys |
|---|---|---|
| Where they live | Config file or `PUSHQ_API_KEYS` | The database (stored as SHA-256 hashes) |
| Create and revoke | Edit the config and restart the server | Console or `/v1/api-keys`, effective immediately |
| Shown in the dashboard | Only as a count ("N more keys are defined in the server config file") | Listed with name, prefix, role and last use |
| `last_used_at` | Not tracked | Tracked |
| First-time setup | If any exist, creating the first dashboard account requires one of the **admin** config keys | Not involved |

To rotate a config-file key, add the new key next to the old one, restart, deploy the new key to your apps, then remove the old one and restart again.

:::caution
A self-hosted server with no config keys and no dashboard account yet accepts anonymous requests as admin. Create the first account (or set `api_keys`) before exposing it. See [Team members](https://pushqueue.dev/docs/guides/team-members/#first-time-setup).
:::

In [SaaS mode](https://pushqueue.dev/docs/self-hosting/saas-mode/) (a self-hosted server running multi-tenant with WorkOS), config-file keys become **operator keys**: they don't belong to any team, they see every team's queues, and teams never see them. Teams create their own keys in the console as described above.

On a self-hosted server's dashboard you can also sign in with an API key instead of an account: on the sign-in page choose **Use an API key instead**. The key is kept for that browser tab only.
