Create a project and issue a key
Projects explains when to create a project; Secret keys and Publishable keys say which key each caller sends.
token is your API key, from Start a workspace, or your session, from Sign in with an emailed code. Both pages also give workspace. Creating a project and issuing keys take the role of an owner or an admin; Roles lists each role’s permissions.
Before you start
- The
apihelper that sends each request, from Platform quickstart. - Your credential,
token, and the workspace to build in,workspace, from Start a workspace.
This page's code continues the script of the pages above: run its blocks after theirs, in the same file. To run them as a script of their own, first copy each helper from the page that defines it, and set each other value, such as a key or an ID, to one you already have.
1. Create a project
Send POST /v1/projects with the workspace and a name of 1 to 120 characters:
const { project } = await api('POST', '/v1/projects', token, {
workspaceId: workspace.id,
name: 'My app',
});
console.log(project.id.slice(0, 4), project.services); // prj_ []
A workspace holds 100 projects; past that, creating one answers quota_exceeded. A role without the permission answers forbidden.
2. Adopt a service
A project reaches a service once it adopts it. Send the service’s code name, such as identityhound:
const adopted = await api(
'POST',
`/v1/projects/${project.id}/services`,
token,
{ service: 'identityhound' },
);
console.log(adopted.adopted, adopted.project.services); // true [ 'identityhound' ]
Send one request for each service the project uses. Agentpost writes the starting config in both environments. Adopting a service the project already has changes nothing and answers adopted: false. This step answers invalid_request for a service Agentpost doesn’t host and lists the hosted services in details.hosted.
3. Issue a secret key
Issue a secret key for the project’s server in its dev environment. name says what the key is for:
const { key } = await api('POST', `/v1/projects/${project.id}/keys`, token, {
name: 'server',
environment: 'dev',
});
console.log(key.slice(0, 8)); // sk_test_
key is shown once: store it where the server reads it. For production, send environment: 'prd'. This step may answer invalid_request.
4. Issue a publishable key
A browser starts a sign-in with a publishable key. Issue one with kind: 'publishable' and the origins the browser code runs on:
const { key: publishableKey } = await api(
'POST',
`/v1/projects/${project.id}/keys`,
token,
{
name: 'web',
environment: 'dev',
kind: 'publishable',
allowedOrigins: ['https://app.example.com', 'http://localhost:3000'],
},
);
console.log(publishableKey.slice(0, 8)); // pk_test_
Origins follow these rules; Update a publishable key changes them later. For a native app, such as an iOS, Android, or Expo app, issue a key of its own with allowNativeApps: true, which may list no origins; Native apps says what it reaches. Issuing the key may answer invalid_request.
Check it worked
List the project’s keys:
const { keys } = await api('GET', `/v1/projects/${project.id}/keys`, token);
const kinds = keys.map((k: { kind: string }) => k.kind);
console.log(kinds); // [ 'secret', 'publishable' ]
Each key’s prefix tells keys apart. The keys reference has every field.