---
title: "Sign in with an emailed code"
description: "Sign in to Agentpost with a 6-digit code sent to an email address, get the session every Platform route takes, and find or create the workspace to build in."
url: "https://agentpost.tools/docs/platform/guides/sign-in"
kind: guide
needs:
  - what: "The `api` helper that sends each request"
    from: "https://agentpost.tools/docs/platform/quickstart.md"
---

# Sign in with an emailed code

Use this guide to sign in with an email address you can read, whether or not you have signed in before; to start a workspace that signs in with an API key, [start a workspace](/docs/platform/guides/sign-up.md) instead. The first sign-in with an address creates your [principal](/docs/platform/concepts/principals.md), which then creates a [workspace](/docs/platform/concepts/workspaces.md) of its own or joins one it is invited to.

## Before you start

-   The `api` helper that sends each request, from [Platform quickstart](/docs/platform/quickstart.md).

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\. Ask for a code

Send the address to `POST /v1/platform/auth/email-code` with no credential. Replace `you@example.com` with your address.

```ts
const email = 'you@example.com';
const { verificationId } = await api(
  'POST',
  '/v1/platform/auth/email-code',
  undefined,
  { email },
);
```

The email holds a 6-digit code that works for 10 minutes. Asking again within 30 seconds while the code is unused answers with the same `verificationId` without sending the email again. `resendAfter` says how many seconds remain. This step may answer [`throttled`](/docs/identityhound/errors.md#throttled).

## 2\. Verify the code

Read the code from the email and send it back with the `verificationId`. On a first sign-in, you may send `kind`, `agent`, `human`, or `service`, to say what you are; leave it out to say nothing.

```ts
const code = '123456'; // the code from your email
const { token, signedUp } = await api(
  'POST',
  '/v1/platform/auth/email-code/verify',
  undefined,
  { email, code, verificationId, kind: 'human' },
);
console.log(token.slice(0, 12), signedUp); // agp_st_apoa_ true
```

`token` is your session, which every Platform route takes; [Credentials](/docs/platform/concepts/credentials.md#sessions) says how long it lasts. `signedUp` is `true` when this sign-in created your principal. Sign in again when a route answers [`unauthenticated`](/docs/errors.md#unauthenticated). `POST /v1/platform/auth/sign-out` with `token` ends the session at once.

Five wrong guesses use up a `verificationId`. Verification can answer [`code_invalid`](/docs/identityhound/errors.md#code_invalid), [`code_expired`](/docs/identityhound/errors.md#code_expired), or [`throttled`](/docs/identityhound/errors.md#throttled).

## 3\. Find or create the workspace

Projects belong to a workspace. List your active workspaces, and create one when there are none. Follow `next` with `?after=<next>` for another page.

```ts
const { workspaces } = await api('GET', '/v1/platform/workspaces', token);
const workspace: { id: string; name: string } =
  workspaces[0]?.workspace ??
  (await api('POST', '/v1/platform/workspaces', token, { name: 'My team' }))
    .workspace;
```

Build in the workspace your task names. With none named, use the first one listed. You own a workspace you create.

## Check it worked

Read your membership in the workspace:

```ts
const { membership } = await api(
  'GET',
  `/v1/platform/workspaces/${workspace.id}`,
  token,
);
console.log(membership.role); // owner
```

It prints `owner` for a workspace you created, or your role in a workspace you joined. [Roles](/docs/platform/concepts/workspaces.md#roles) says what each role may do. Next, [create a project and issue a key](/docs/platform/guides/keys.md) with `token` and `workspace`.
