IdentityHound quickstart
Sign a principal in and check its access through HTTP or the SDK. The script runs start to finish on its own: it reads the sign-in code from your project’s test inbox. Pick the same tab throughout, and run its blocks and the shared blocks in order as one TypeScript script in Node 24: later blocks use values from earlier ones. The API reference has every operation.
For the SDK variant, install the client:
npm i @agentpost-tools/identityhound-sdk
Both variants use the Platform quickstart’s api helper to read the Platform test inbox.
Before you start
- The
apihelper, which sends a request and throws on an error, from Platform quickstart. - A
devsecret key,key, for a project that adopted IdentityHound, from Platform quickstart.
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. Configure your project
IdentityHound answers for your project at /v1/identity. Your server sets its config with the secret key: the resources your product has and their actions, what each role may do, and the entitlements you sell. Here a report can be viewed, edited, shared, and exported, and exporting needs the reports.export entitlement.
The shared identity helper sends HTTP calls and is also used by the guides that follow:
const identity = (
method: string,
path: string,
body?: unknown,
headers?: Record<string, string>,
) => api(method, `/v1/identity${path}`, key, body, headers);
Read the config, change the settings you need, and send the whole config back. Use the version you read to catch another write before yours. Agentpost supplies the starting config when your project adopts IdentityHound. The example uses its admin and member roles.
const report = {
actions: ['view', 'edit', 'share', 'export'],
requiresEntitlement: { export: 'reports.export' },
};
const exportReports = { description: 'Export reports' };
function addPermissions<P extends string>(
permissions: readonly P[],
...added: P[]
): P[] {
return [...new Set([...permissions, ...added])];
}
const { config: settings } = await identity('GET', '/config');
settings.signUp.mode = 'open';
settings.signIn.emailCode.enabled = true;
settings.urls.invitationAccept = 'https://app.example.com/invitations/accept';
settings.resourceTypes.report = report;
settings.entitlements['reports.export'] = exportReports;
settings.roles.admin.permissions = addPermissions(
settings.roles.admin.permissions,
'report:*',
);
settings.roles.member.permissions = addPermissions(
settings.roles.member.permissions,
'report:view',
'report:edit',
'report:export',
);
await identity(
'PUT',
'/config',
{
...settings,
roles: {
...settings.roles,
owner: {
...settings.roles.owner,
permissions: addPermissions(
settings.roles.owner?.permissions ?? [],
'report:*',
),
},
},
},
{ 'If-Match': String(settings.version) },
);
import { createClient } from '@agentpost-tools/identityhound-sdk';
const client = createClient({ token: key });
const { config: settings } = await client.config.get();
settings.signUp.mode = 'open';
settings.signIn.emailCode.enabled = true;
settings.urls.invitationAccept = 'https://app.example.com/invitations/accept';
settings.resourceTypes.report = report;
settings.entitlements['reports.export'] = exportReports;
settings.roles.admin.permissions = addPermissions(
settings.roles.admin.permissions,
'report:*',
);
settings.roles.member.permissions = addPermissions(
settings.roles.member.permissions,
'report:view',
'report:edit',
'report:export',
);
await client.config.put(
{
...settings,
roles: {
...settings.roles,
owner: {
...settings.roles.owner,
permissions: addPermissions(
settings.roles.owner?.permissions ?? [],
'report:*',
),
},
},
},
settings.version,
);
Send the version, audit fields, and built-in role details as read if you include them. A provider’s client secret stays in place when you send the provider back without it. Config has the field rules and errors.
2. Sign a principal in
A principal signs in with an emailed code: POST /auth/email-code sends it, and POST /auth/email-code/verify takes it back with the verificationId. Sessions has the code’s limits.
This script reads the code from your project’s Platform test inbox:
async function testInbox(to: string) {
const { messages } = await api(
'GET',
`/v1/test-inbox?to=${encodeURIComponent(to)}`,
key,
);
return messages.at(-1);
}
async function signIn(email: string) {
const { verificationId } = await identity('POST', '/auth/email-code', {
email,
});
const { data } = await testInbox(email);
return identity('POST', '/auth/email-code/verify', {
email,
code: data.code,
verificationId,
});
}
const ana = await signIn('ana@acme.test');
console.log(ana.signedUp, ana.principal.id); // true prn_…
async function signIn(email: string) {
const { verificationId } = await client.auth.requestCode({ email });
const { data } = await testInbox(email);
return client.auth.verifyCode({
email,
code: data.code,
verificationId,
});
}
const ana = await signIn('ana@acme.test');
console.log(ana.signedUp, ana.principal.id); // true prn_…
With a real address, whoever holds the inbox reads the code from it, whether an agent or a human. From a browser, your app starts the sign-in with a publishable key instead of the secret key, as Add email sign-in shows.
3. Create a workspace
A principal’s own requests carry its session token or API key. Ana creates a workspace, Acme, and becomes its owner:
const { workspace: acme } = await api(
'POST',
'/v1/identity/workspaces',
ana.token,
{ name: 'Acme' },
);
console.log(acme.name); // Acme
const { workspace: acme } = await client
.withToken(ana.token)
.workspaces.create({
name: 'Acme',
});
console.log(acme.name); // Acme
4. Check access
Before Ana acts on a report, your server asks IdentityHound with POST /access/check, naming the principal, the action, and the resource. Each answer has allowed and a reason; Access checks explains them:
const check = (action: string) =>
identity('POST', '/access/check', {
principalId: ana.principal.id,
action,
resource: { type: 'report', id: 'r1', workspaceId: acme.id },
});
const check = (action: string) =>
client.access.check({
principalId: ana.principal.id,
action,
resource: { type: 'report', id: 'r1', workspaceId: acme.id },
});
A denied answer’s reason says what to do. The errors page lists every reason and what to do for each.
Check it worked
Check whether Ana may edit and export a report:
const edit = await check('edit');
console.log(edit.allowed, edit.reason); // true workspace_role
const exporting = await check('export');
console.log(exporting.allowed, exporting.reason); // false entitlement_missing
Ana’s owner role covers both actions, but Acme holds no reports.export entitlement, so the export is denied. To go live, issue a prd key, read the config, set your real URLs, send it back, and sign principals in at their real addresses. Next, Grant a plan grants Acme the missing entitlement.