Skip to main content

Install the client plugin

Add the invite client plugin to your Better Auth client configuration:
client.ts
import { createAuthClient } from "better-auth/client";
import { inviteClient } from "better-auth/client/plugins";

export const authClient = createAuthClient({
  baseURL: "http://localhost:3000", // Your API base URL
  plugins: [inviteClient()],
});

Available methods

The invite client plugin automatically generates type-safe methods for all invite endpoints. Here’s what becomes available on your client:

Create invitation

const result = await authClient.invite.create({
  email: "[email protected]",
  role: "member",
});

Activate invitation

const result = await authClient.invite.activate({
  token: "invitation-token",
  callbackURL: "/dashboard",
});

Get invitation details

const invite = await authClient.invite.get({
  token: "invitation-token",
});

Cancel invitation

const result = await authClient.invite.cancel({
  token: "invitation-token",
});

Reject invitation

const result = await authClient.invite.reject({
  token: "invitation-token",
});

Type inference

The invite client plugin provides full type inference based on your server configuration:
import type { InferServerPlugin } from "better-auth/client";
import type { invite } from "better-auth/plugins";

// Your client methods are fully typed
type InvitePlugin = InferServerPlugin<typeof invite>;
The client plugin automatically infers available endpoints and their request/response types from your server configuration.

Framework integration

The invite client works seamlessly with Better Auth’s framework integrations:
import { useSession } from "better-auth/react";
import { authClient } from "./client";

function InviteButton() {
  const { data: session } = useSession();

  const handleInvite = async () => {
    try {
      await authClient.invite.create({
        email: "[email protected]",
        role: "member",
      });
      alert("Invitation sent!");
    } catch (error) {
      console.error("Failed to send invite:", error);
    }
  };

  if (session?.user.role !== "admin") {
    return null;
  }

  return <button onClick={handleInvite}>Send Invite</button>;
}

Error handling

Handle errors from invite operations using try-catch blocks:
try {
  await authClient.invite.create({
    email: "[email protected]",
    role: "admin",
  });
} catch (error) {
  if (error.errorCode === "INSUFFICIENT_PERMISSIONS") {
    alert("You don't have permission to create invites");
  } else if (error.errorCode === "INVALID_TOKEN") {
    alert("The invitation token is invalid or expired");
  } else {
    alert("An error occurred");
  }
}

Common error codes

  • INSUFFICIENT_PERMISSIONS - User lacks permission for the operation
  • INVALID_TOKEN - Token is invalid, expired, or already used
  • INVALID_EMAIL - Email doesn’t match the invite recipient
  • CANT_ACCEPT_INVITE - User cannot accept this invitation
  • CANT_REJECT_INVITE - User cannot reject this invitation
  • NO_USES_LEFT_FOR_INVITE - Invite has reached maximum uses
  • INVITER_NOT_FOUND - The user who created the invite no longer exists

URL-based activation

For invitation links (callback flow), you can use the activation callback endpoint directly:
// The user clicks a link like:
// https://yourapp.com/api/auth/invite/TOKEN?callbackURL=/dashboard

// Better Auth automatically handles this and redirects the user
// to the callbackURL with the token in a cookie
If you need to activate programmatically after the user follows a link:
// In your page component
const searchParams = new URLSearchParams(window.location.search);
const token = searchParams.get("token");

if (token) {
  const result = await authClient.invite.activate({
    token,
    callbackURL: window.location.pathname,
  });

  if (result.action === "SIGN_IN_UP_REQUIRED") {
    // Redirect user to sign in/up page
    window.location.href = result.redirectTo;
  } else {
    // User is logged in, redirect to dashboard
    window.location.href = result.redirectTo;
  }
}

Next steps

Creating invites

Learn how to create invitations

Activating invites

Handle invitation activation flow

Build docs developers (and LLMs) love