Skip to main content

Overview

TalkifAI supports two authentication methods:
  1. Session-based authentication — For Studio UI and logged-in users (via Better Auth)
  2. API keys — For server-to-server API access
The authentication method you use depends on your use case:

Session-Based Authentication

For applications where users log in to TalkifAI Studio, authentication is handled automatically via Better Auth session cookies.

How It Works

  1. User logs in via TalkifAI Studio
  2. Session cookie is set automatically
  3. All API requests include the session cookie
  4. Backend validates session via auth.api.getSession()

Example (Server-Side)

Example (Client-Side)


API Key Authentication

For server-to-server communication (e.g., marketing site → Studio API, external backends), use API keys.

Generating an API Key (Via Studio UI)

Navigation: Studio → Organization → API Keys URL: /organization/api-key
Owner access required: Only organization owners can generate API keys. Admins and Members cannot access this feature.
Step-by-Step Process:
  1. Navigate to API Keys Page
    • Go to Studio → Organization (sidebar)
    • Click API Keys in the organization settings
  2. Enter Key Details
    • Key Name (required, max 50 characters)
      • Example: “Production Server”, “Marketing Site”, “Dev Testing”
    • Organization is automatically selected from your active session
  3. Click “Generate API Key”
    • Button shows loading spinner while processing
    • Backend creates key via Better Auth
  4. Copy the Key Immediately ⚠️
    • The full key is shown only once in a dark gray box
    • Click Copy button to copy to clipboard
    • Shows “Copied!” confirmation for 2 seconds
    • Important: You cannot view the key again after leaving the page
  5. Key Details Shown
    • Created date
    • Expires date (1 year from creation)
    • API endpoints that accept this key:
      • Public Chat API: https://api.talkifai.dev/v1/chat
      • Data Export API: https://api.talkifai.dev/api/export
Security Warning: API keys grant full access to your organization.
  • ✅ Copy the key immediately and store in a password manager
  • ✅ Use environment variables in your application
  • ❌ Never commit to version control (add to .gitignore)
  • ❌ Never expose in client-side code
  • ❌ Never share via email or chat
After Generation:
  • Key appears in the “Existing Keys” list on the right
  • Shows: Name, prefix (e.g., abc123••••••••), created date, expires date, status
  • Can be revoked anytime by clicking Delete (trash icon)

API Key Format

TalkifAI API keys are random strings (no special prefix like tk_live_). Example: abc123def456... (64 characters)

Using API Keys

Include the API key in the x-api-key header:

API Key Features


Organization Context

All API operations are scoped to your active organization.

Session-Based (Studio Users)

The active organization is stored in the session:
Users can switch organizations in the Studio UI, which updates activeOrganizationId.

API Key Authentication

API keys are automatically scoped to the organization they were created in:
All requests with this key automatically operate within that organization.

Environment Variables

Store credentials securely as environment variables:
Never commit .env files to version control. Add them to .gitignore:

Verifying Your API Key

Test your API key with a simple request:
Success Response:
Error Response (Invalid Key):

Revoking API Keys

To revoke a key:
  1. Go to Studio → Organization → API Keys
  2. Find the key in the “Existing Keys” list (right column)
  3. Click Delete (trash icon with red button)
  4. Confirm deletion in the dialog: “Are you sure you want to delete this API key?”
Revoked keys immediately return 401 Unauthorized on all requests.
Irreversible Action: Once revoked, the key cannot be recovered. You’ll need to generate a new key and update your applications.

Security Best Practices

Rotate Keys Regularly

Generate new API keys every 90 days. Delete old keys after rotation.

Use Separate Keys

Use different keys for development, staging, and production environments.

Monitor Active Keys

Check Organization → API Keys regularly. Revoke unused keys immediately.

Never Log Keys

Never log API keys in application logs. Redact sensitive headers.

Use Environment Variables

Store keys in environment variables, not in code or config files.

Limit Key Permissions

Create separate keys for different services with minimal required access.

Error Codes