Managing projects
A project is a separate workspace inside your organisation. Each project has its own policy (allowed regions, max tokens), budget, environments and API keys. Use projects to keep dev, staging and production apart, or to separate products. (New to the idea? Read Projects first.)
Create, list, update and delete
// List
const { projects } = await client.projects.list();
// Create
const project = await client.projects.create({
name: 'My App',
slug: 'my-app', // optional URL-safe identifier
description: 'Production AI', // optional
});
// Update
await client.projects.update(project.id, { name: 'My App v2' });
// Delete
await client.projects.delete(project.id);
A slug is a short, web-safe name, like my-app (no spaces or odd symbols).
What if my client uses a project-scoped API key?
A client made with a project-scoped key works inside that one project only:
client.projects.list()returns just the key's project.client.project(id)works for that project alone. Any other project is "not found" (404).- The key can read the project's settings. But creating, updating or deleting projects, and changing a project's settings, needs an owner's or admin's session (
403). - Organisation-level calls, such as
client.routing.getConfig()andclient.keys, are refused with403. client.requests.list(),client.routing.listHistory()andclient.usagecover the key's project only.client.requests.get()orclient.routing.getDecision()on a request of another project (or of no project) is "not found" (404).
Work inside one project: the sub-client
Once you have a project ID, client.project(id) gives you a sub-client for everything in that project:
const proj = client.project('proj_abc123');
Policy
The policy applies to every request of the project: see Policy enforcement.
set() replaces the whole policyA field you leave out is cleared. So start from get(), then change what you need.
const policy = await proj.policy.get();
await proj.policy.set({
...policy,
maxTokens: 4096,
allowedRegions: ['EU'],
});
Budget
set() replaces the whole budgetSend all three fields. A missing hardLimit turns the hard limit off.
See Budget enforcement.
await proj.budget.set({
monthlyUsd: 500, // monthly cap, per calendar month (UTC)
alertPct: 80, // stored; budget alerts are not sent yet
hardLimit: true, // refuse the project's requests (402 budget_exceeded) once the cap is spent
});
const budget = await proj.budget.get();
console.log(budget.currentSpend, budget.monthlyUsd);
Environments
const { environments } = await proj.environments.list();
const { id } = await proj.environments.create({
name: 'Staging',
slug: 'staging',
isProduction: false,
});
await proj.environments.delete(id);
Project usage
How much the project has used. The number is how many hours back to look:
const overview = await proj.usage.overview(24); // last 24 h
const series = await proj.usage.series(168); // last 7 days
const breakdown = await proj.usage.breakdown(24);