Skip to content

DocsAdd AgentID to Clerk

Add AgentID to Clerk

Scroll down and follow each step in order.

All docs
On this page

Recommended: set it up from your project

The CLI detects or lets you select the Clerk application, completes AgentID registration in the browser, turns on Clerk’s AgentID connection with your credentials and validates it. It preserves existing sign-in state on reruns.

terminal
npx @agentmail/agentid-cli init

The CLI keeps credentials in your project and leaves hosted-provider sign-in disabled until you opt in. See the CLI guide for scope selection, verification and cleanup. The manual steps below remain the fallback.

Step 1 of 8

Add AgentID in Clerk

Open Clerk dashboard
ConfigureUser & authenticationSSO connectionsAdd connectionFor all users
Clerk's SSO connections page with the Add connection menu open on For all users.

Search for AgentID and choose the built-in AgentID, not Create custom provider.

Clerk's Add connection for all users panel, searched for AgentID, with the built-in AgentID provider above the Create custom provider option.

Trying it on a development instance? Clerk’s shared credentials work as is: turn on Enable for sign-up and sign-in, click Enable connection and skip to step 6. Production needs your own AgentID application, so keep going.

Don’t want a visible AgentID button? See step 8.

Step 2 of 8

Turn on custom credentials

Turn on Enable for sign-up and sign-in and Use custom credentials. Copy the Redirect URI. You’ll paste it in the next step.

Clerk's AgentID OAuth page with Enable for sign-up and sign-in and Use custom credentials switched on, empty Client ID and Client Secret fields, and the Redirect URI with a copy button.

Don’t want a visible AgentID button? See step 8.

Step 3 of 8

Create an AgentID application

Open AgentID console

Click Create application. Choose Clerk as the auth provider, enter your app name and paste Clerk’s Redirect URI.

Click Create application.

Step 4 of 8

Paste both keys into Clerk

Copy Client ID and Client secret. The secret is shown only once. If you lose it, make a new one from your application’s Settings.

The AgentID console's Application credentials window with copy buttons for the client ID and client secret.

Paste them into Clerk’s Client ID and Client Secret fields.

Click Save in the bar at the bottom of the page, then Enable connection at the top right. Clerk turns on PKCE for AgentID by itself.

Don’t want a visible AgentID button? See step 8.

Step 5 of 8

Add owner scopes (optional)

Clerk always asks for openid email profile. Need the name and email of the person who owns the agent? Under Scopes, type each of these and click Add, then Save:

Owner scopes
owner_profileowner_email
Clerk's Scopes field with openid, email and profile, plus owner_profile and owner_email added, and the Save bar.

Owner scopes also need the App: Share Owner permission on the agent’s AgentMail API key. Without it, the agent can’t finish the sign-in alone, and the organization owner has to approve it from their AgentMail account.

Step 6 of 8

Let agents finish sign-up

Clerk’s bot sign-up protection shows new sign-ups a Cloudflare Turnstile check. Agents can’t pass it, so their first sign-in stops there. Turn it off:

ConfigureProtectRulesBot sign-up protectionManage

Switch off Enable and click Save.

Clerk's Bot sign-up protection rule, powered by Cloudflare Turnstile, with its Manage button.

Step 7 of 8

Test the sign-in

Open your sign-in page.

AgentID

You land on an AgentID waiting page. If the agent’s browser is already signed in to AgentID, it continues on its own. If not, the page shows the agent one command to run. More in AgentID browser enrollment.

A Clerk sign-in page with AgentID and Google buttons above the email field.

Using your own sign-in button? Copy this:

Sign in
signIn.sso({
  strategy: 'oauth_agentid',
  redirectCallbackUrl: '/sso-callback',
  redirectUrl: '/',
})

Don’t want a visible AgentID button? See step 8.

Step 8 of 8

Add an initiate login URI

Don’t want a visible AgentID button? Add a page that starts the AgentID sign-in as soon as it loads. Like the button in step 7, it returns through /sso-callback.

Create it as a new page in your app at /login/agentid. Use the version for your framework below. Each code block’s title bar shows the file to create.

Next.js App Router

app/login/agentid/page.tsx
'use client'

import { useSignIn } from '@clerk/nextjs'
import { useEffect, useRef } from 'react'

export default function AgentIdSignIn() {
  const { signIn } = useSignIn()
  const started = useRef(false)

  useEffect(() => {
    if (started.current) return // effects can run more than once
    started.current = true
    void signIn.sso({
      strategy: 'oauth_agentid',
      redirectCallbackUrl: '/sso-callback',
      redirectUrl: '/',
    })
  }, [signIn])

  return null
}

Next.js Pages Router

pages/login/agentid.tsx
import { useSignIn } from '@clerk/nextjs'
import { useEffect, useRef } from 'react'

export default function AgentIdSignIn() {
  const { signIn } = useSignIn()
  const started = useRef(false)

  useEffect(() => {
    if (started.current) return // effects can run more than once
    started.current = true
    void signIn.sso({
      strategy: 'oauth_agentid',
      redirectCallbackUrl: '/sso-callback',
      redirectUrl: '/',
    })
  }, [signIn])

  return null
}

React

src/AgentIdSignIn.tsx
// Route it at /login/agentid, for example:
// <Route path="/login/agentid" element={<AgentIdSignIn />} />
import { useSignIn } from '@clerk/react'
import { useEffect, useRef } from 'react'

export default function AgentIdSignIn() {
  const { signIn } = useSignIn()
  const started = useRef(false)

  useEffect(() => {
    if (started.current) return // effects can run more than once
    started.current = true
    void signIn.sso({
      strategy: 'oauth_agentid',
      redirectCallbackUrl: '/sso-callback',
      redirectUrl: '/',
    })
  }, [signIn])

  return null
}

JavaScript

login/agentid.js
// Load this script on the page served at /login/agentid
import { Clerk } from '@clerk/clerk-js'

const clerk = new Clerk(import.meta.env.VITE_CLERK_PUBLISHABLE_KEY)
await clerk.load()

await clerk.client.signIn.authenticateWithRedirect({
  strategy: 'oauth_agentid',
  redirectUrl: '/sso-callback', // calls clerk.handleRedirectCallback()
  redirectUrlComplete: '/',
})

In AgentID console

Agents connect to your app from AgentMail by opening its initiate login URI. Your sign-in page works as one: AgentID tells the agent to choose AgentID there.

Open your application, then Settings › Edit. Paste the page’s address into Initiate login URL, for example https://yourapp.com/login/agentid, or your sign-in page’s if you kept the button. Click Save changes.

On Supabase instead? Add AgentID to Supabase adds AgentID to Supabase Auth as a custom provider.