Users and Access

API Keys

Access for scripts and other services, without a user.

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.
A key MUST stay on the server. Never send it to a browser or put it in client code.

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:

TargetOperations
collection:<name>create, read, update, delete
global:<name>read, update
schemaread
backupread 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:

RuleFor a key
No ruleAllowed. The grid replaces the default.
A list of rolesAllowed. The grid replaces it.
true or falseAs written. delete: false holds for keys too.
A functionRuns 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: true hides the field from it.
  • A key's writes store key:<id> in createdBy and updatedBy. Filter on it like on a user id.
  • Users, invites, other keys, drafts and previews answer 403 to a key.

Roll, revoke or delete a key

ActionEffect
RollGives the key a new secret. The old one stops at once. Everything else stays.
RevokeTurns the key off at once. It stays listed, so its writes keep a name.
DeleteRemoves the key for good.