API Keys
An API key lets a script or another service use the HTTP API without signing in. Examples: a build that reads private content, or a shop that syncs its products. This page shows how to create a key, use it, and limit what it may do.
- A key is its own caller, not a user. It records which admin created it, but does not act as them.
- A key reads published content only, never drafts.
- Tenon stores only a hash of the key. The admin shows the key once, when you create it.
Create a key
Admins create keys under Manage → API keys. Each key has a name, an optional expiry of up to 365 days, and a grid of permissions per target:
| Target | Operations |
|---|---|
collection:<name> | create, read, update, delete |
global:<name> | read, update |
schema | read |
backup | read to export, update to import |
collection:media is the media library. An upload needs create on it.
A request to a target the key does not hold answers 403. A collection you add later is not part of existing keys.
Use a key
Send the key in the x-api-key header, or pass it to the client:
import { createClient } from "tenoncms/client";
const client = createClient({
baseUrl: "https://example.com",
apiKey: process.env.TENON_API_KEY,
});
await client.create("products", { name: "Watch", sku: "W-1" });
A key that is unknown, revoked or expired answers 401.
Keys and rules
The grid is checked first. Then the target's access rule decides:
| Rule | For a key |
|---|---|
| No rule | Allowed. The grid replaces the default. |
| A list of roles | Allowed. The grid replaces it. |
true or false | As written. delete: false holds for keys too. |
| A function | Runs with user: null and apiKey. |
A function can narrow what a key may do:
access: {
update: ({ user, apiKey }) => !!user || apiKey?.name === "Shop sync",
},
- Field rules treat a key like a visitor. A list of roles or
private: truehides the field from it. - A key's writes store
key:<id>increatedByandupdatedBy. Filter on it like on a user id. - Users, invites, other keys, drafts and previews answer
403to a key.
Roll, revoke or delete a key
| Action | Effect |
|---|---|
| Roll | Gives the key a new secret. The old one stops at once. Everything else stays. |
| Revoke | Turns the key off at once. It stays listed, so its writes keep a name. |
| Delete | Removes the key for good. |
Related resources
- Access control: roles and rules.
- HTTP API and client: what is public, and how to call the API.
- Headless: read and write from another origin with a key.