Authentication
Configure a server SDK once with your project API key and project ID using the quick start or your SDK reference. Other examples reuse that client; it sends the authentication header for you. Without an SDK, send the key in the X-Api-Key header of every request; see the API reference.
import { CalmLensServerClient } from "calmlens/server";
const client = new CalmLensServerClient({
apiKey: (process.env.CALMLENS_API_KEY ?? ""),
projectId: (process.env.CALMLENS_PROJECT_ID ?? ""),
});
const page = await client.assets.list();
console.log(page);
Create and protect keys
Create keys under Portal › API keys. In the API key created dialog, select Copy API key and save the complete value to your server configuration or secret manager before selecting Done. The list shows the key's prefix for identification. If you closed the dialog without saving the key, rotate it.
- Keep keys on a server or in a secret manager, never in browser bundles or source control.
- Give each integration its own named key so it can be revoked independently.
- Treat a revoked or expired key as unusable and rotate clients before expiry.
- A key is project-scoped. The project in the URL must match the key's project.
Rotate a key
- Create a replacement with the permissions your integration needs.
- Update the secret in your server environment and deploy it.
- Verify a request succeeds with the replacement, then revoke the old key.
Choose permissions
| Integration needs | Grant |
|---|---|
| Submit content | Upload assets (assets:create) in the relevant project or folders. |
| Read reports | View assets (assets:read) in the relevant scope. |
| Fetch private files | Download assets (assets:download). |
| Upload public assets, change an asset's visibility, or give browsers private access with signed URLs | Share assets (assets:share), in addition to the permission for the upload or change itself. |
Use Members and permissions for custom folder scopes.
Roles
A key, like a member, gets a standard role or custom permissions. Editor suits an integration that uploads and moderates content: it can manage assets, folders and hash lists, view and decide the review queue, and view the upload presets, metadata fields, lenses, policies and members it works with. Configurator suits a setup or deploy script: it can do everything Editor can, and also manage lenses, upload presets, policies (including choosing the project's policy), metadata fields and project settings, without managing members, API keys, connectors or the project itself. Viewer only reads. Administrator holds every project permission. A key receives the events whose permission it holds, so an Editor key receives asset.review-requested but not API key or connector events.
| Permission | Administrator | Configurator | Editor | Viewer |
|---|---|---|---|---|
| View assets assets:read | Yes | Yes | Yes | Yes |
| Download assets assets:download | Yes | Yes | Yes | Yes |
| Upload assets assets:create | Yes | Yes | Yes | No |
| Edit and move assets assets:update | Yes | Yes | Yes | No |
| Delete assets assets:delete | Yes | Yes | Yes | No |
| Share assets assets:share | Yes | Yes | Yes | No |
| Browse folders folders:read | Yes | Yes | Yes | Yes |
| Create folders folders:create | Yes | Yes | Yes | No |
| Rename folders folders:update | Yes | Yes | Yes | No |
| Delete empty folders folders:delete | Yes | Yes | Yes | No |
| View project settings settings:read | Yes | Yes | No | No |
| Change project settings settings:write | Yes | Yes | No | No |
| View members members:read | Yes | Yes | Yes | Yes |
| Invite and manage members members:write | Yes | No | No | No |
| View API keys api-keys:read | Yes | No | No | No |
| Manage API keys api-keys:write | Yes | No | No | No |
| View webhooks and connectors connectors:read | Yes | No | No | No |
| Manage webhooks and connectors connectors:write | Yes | No | No | No |
| View metadata fields metadata:read | Yes | Yes | Yes | No |
| Manage metadata fields metadata:write | Yes | Yes | No | No |
| View lenses lenses:read | Yes | Yes | Yes | Yes |
| Manage lenses lenses:write | Yes | Yes | No | No |
| View upload presets upload-presets:read | Yes | Yes | Yes | No |
| Manage upload presets upload-presets:write | Yes | Yes | No | No |
| View hash lists hash-lists:read | Yes | Yes | Yes | No |
| Manage hash lists hash-lists:write | Yes | Yes | Yes | No |
| View moderation policies policies:read | Yes | Yes | Yes | Yes |
| Manage moderation policies policies:write | Yes | Yes | No | No |
| View reviews reviews:read | Yes | Yes | Yes | No |
| Review content reviews:write | Yes | Yes | Yes | No |
| Manage project project:manage | Yes | No | No | No |
A permission that manages a feature includes viewing it, and Manage project includes every project configuration permission. For anything else, such as an Editor key that also manages lenses, use custom permissions. The portal asks you to confirm before saving a key that manages the project, its members or API keys, or that can delete assets or folders anywhere in the project.
CDN access
Public assets need no authentication. Private assets need a short-lived signed URL, which suits browsers and other untrusted clients, or a key with Download assets; see Asset URLs.
Authentication failures
The key is checked before the request is validated, so an authentication error comes first. Authentication errors use the same JSON body as other request errors. Branch on its code:
| Error | What to do |
|---|---|
| 401 api-key/missing | Send your project API key in the X-Api-Key header. |
| 401 api-key/invalid | The key is unknown, revoked or expired. Check for typos or whitespace, or create a new key under Portal › API keys. |
| 403 api-key/project-mismatch | The key belongs to a different project. Use a key created for the project in the URL, or the project that owns the asset or folder. |
| 403 auth/permission-denied | The key is valid but lacks a permission the request needs, such as Delete assets for the asset's folder. When CalmLens knows which one, details.permission names it, such as assets:delete, with details.folderId for a folder grant, and details.role names the lowest role that includes it, such as configurator for lenses:write. Give the key that permission under Portal › API keys, or use a key that has it. |
| 403 api-key/not-accepted | API keys are accepted only by the public API under https://api.calmlens.com/v1. Send the request to the documented endpoint instead of a portal-only address. |
Do not retry 401 or 403 responses with the same key; fix the configuration first.
A 403 with the plain text body error code: 1010 isn't an authentication error. The request was refused before its key was checked, because of its User-Agent. Clients must send a User-Agent that names your app; the SDKs do this for you.