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. The complete key is shown only when it is created.
- 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 expiration.
- 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. 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 | Editor | Viewer |
|---|---|---|---|
| View assets assets:read | Yes | Yes | Yes |
| Download assets assets:download | Yes | Yes | Yes |
| Upload assets assets:create | Yes | Yes | No |
| Edit and move assets assets:update | Yes | Yes | No |
| Delete assets assets:delete | Yes | Yes | No |
| Share assets assets:share | Yes | Yes | No |
| Browse folders folders:read | Yes | Yes | Yes |
| Create folders folders:create | Yes | Yes | No |
| Rename folders folders:update | Yes | Yes | No |
| Delete empty folders folders:delete | Yes | Yes | No |
| View project settings settings:read | Yes | No | No |
| Change project settings settings:write | Yes | No | No |
| View members members:read | Yes | Yes | Yes |
| Invite and manage members members:write | Yes | No | No |
| View API keys api-keys:read | Yes | No | No |
| Manage API keys api-keys:write | Yes | No | No |
| View webhooks and connectors connectors:read | Yes | No | No |
| Manage webhooks and connectors connectors:write | Yes | No | No |
| View metadata fields metadata:read | Yes | Yes | No |
| Manage metadata fields metadata:write | Yes | No | No |
| View lenses lenses:read | Yes | Yes | Yes |
| Manage lenses lenses:write | Yes | No | No |
| View upload presets upload-presets:read | Yes | Yes | No |
| Manage upload presets upload-presets:write | Yes | No | No |
| View hash lists hash-lists:read | Yes | Yes | No |
| Manage hash lists hash-lists:write | Yes | Yes | No |
| View moderation policies policies:read | Yes | Yes | Yes |
| Manage moderation policies policies:write | Yes | No | No |
| View reviews reviews:read | Yes | Yes | No |
| Review content reviews:write | Yes | Yes | No |
| Manage project project:manage | Yes | 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.
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. 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.