Skip to main content
This feature is currently in Early Availability. Contact support to enable this feature.

Prerequisites

Before integrating the embedded identity flow, ensure the following are configured:
  1. Create an Application - Define the target website users will authenticate to via the Anchor Dashboard or API
  2. Configure Authentication Flows - Set up the supported login methods.

Overview

1

Generate a Token

Create a one-time token that authorizes the identity creation flow.
import Anchorbrowser from 'anchorbrowser';

const anchorClient = new Anchorbrowser();

// Generate a token for identity creation
const tokenResponse = await anchorClient.applications.createIdentityToken(
    'your-application-id',
    { callbackUrl: 'https://your-app.com/identity-callback' }
);

console.log(tokenResponse.token);
// Use this token to redirect the user
The callbackUrl must use HTTPS. Store the token securely and use it immediately - tokens are single-use and expire after 15 minutes.
2

Redirect User to Identity Creation

Redirect the user to the Anchor identity creation page with the generated token:
https://app.anchorbrowser.io/identity/create?token={token}
function CreateIdentityButton({ token }) {
  const handleClick = () => {
    window.location.href = `https://app.anchorbrowser.io/identity/create?token=${token}`;
  };

  return <button onClick={handleClick}>Connect Your Account</button>;
}
The user will be guided through the authentication process for the target website configured in the application.
3

Handle the Callback

After the user successfully creates an identity, Anchor redirects them to the callbackUrl with the identity ID:
https://your-app.com/identity-callback?identityId={identity_id}
app.get('/identity-callback', async (req, res) => {
  const { identityId } = req.query;

  if (!identityId) {
    return res.status(400).send('Missing identity ID');
  }

  // Store the mapping between the user and the Anchor identity
  await saveUserIdentityMapping(req.user.id, identityId);

  // Optionally, update the identity with an external user ID

  res.redirect('/dashboard?connected=true');
});
4

Update Identity Metadata (Optional)

Update the identity with additional metadata to maintain a mapping between users and their Anchor identities:
import Anchorbrowser from 'anchorbrowser';

const anchorClient = new Anchorbrowser();

// Update identity with external user ID
const updatedIdentity = await anchorClient.identities.update('your-identity-id', {
  name: 'User Display Name',
  metadata: {
    externalUserId: 'your-user-id-123',
    plan: 'premium',
    connectedAt: new Date().toISOString(),
  },
});
5

Create Authenticated Sessions

Once an identity ID is available, use it to create authenticated browser sessions:
import Anchorbrowser from 'anchorbrowser';

const anchorClient = new Anchorbrowser();
const identityId = "your-identity-id";
const session = await anchorClient.sessions.create({
    // Recommended settings for authenticated sessions.
    session: {
        proxy: {
            active: true,
        }
    },
    browser: {
        captcha_solver: {
            active: true,
        },
        extra_stealth: {
            active: true,
        }
    },

    // Identity to authenticate with.
    identities: [{ id: identityId }]
});

console.log(session.data.id);
The browser session will automatically be authenticated to the target website using the stored identity.

Complete Integration Example

Here’s a complete example showing the full flow:
import express from 'express';
import Anchorbrowser from 'anchorbrowser';

const app = express();
const anchorClient = new Anchorbrowser();

const APPLICATION_ID = 'your-application-id';

// Step 1: Initiate identity creation
app.post('/api/connect-account', async (req, res) => {
  const userId = req.user.id;

  // Generate token with callback URL
  const tokenResponse = await anchorClient.applications.createIdentityToken(APPLICATION_ID, {
    callbackUrl: `https://your-app.com/api/identity-callback`,
  });

  // Store token-to-user mapping for callback verification
  await storeTokenMapping(tokenResponse.tokenHash, userId);

  // Return redirect URL to frontend
  res.json({
    redirectUrl: `https://app.anchorbrowser.io/identity/create?token=${tokenResponse.token}`,
  });
});

// Step 2: Handle callback after identity creation
app.get('/api/identity-callback', async (req, res) => {
  const { identityId } = req.query;

  // Save identity mapping
  await saveIdentityMapping(req.user.id, identityId);

  // Update identity with external reference
  await anchorClient.identities.update(identityId, {
    metadata: { externalUserId: req.user.id },
  });

  res.redirect('/dashboard?connected=true');
});

// Step 3: Use identity in browser sessions
app.post('/api/run-automation', async (req, res) => {
  const identityId = await getUserIdentity(req.user.id);

  const session = await anchorClient.sessions.create({
    browser: {
      identities: [{ id: identityId }],
    },
  });

  // Run your automation with the authenticated session
  // ...

  res.json({ sessionId: session.id });
});

Best Practices

Secure Token Handling

Generate tokens server-side and never expose your API key to the frontend. Tokens are single-use and should be used immediately.

Store Identity Mappings

Maintain a mapping between your users and their Anchor identity IDs in your database for future session creation.

Use Metadata

Store your external user ID in the identity metadata to easily correlate identities with your users.

Handle Errors

Implement proper error handling for cases where identity creation fails or the user cancels the flow.