Skip to content

Authentication

beginner

How to authenticate with Engramma: API keys, JWT Bearer tokens, refresh flow, and MFA setup.

Authentication methods

Engramma supports two authentication methods. Choose based on your use case:

MethodBest forHeader
API KeyServer-to-server, SDKs, CI/CDX-API-Key: nx_live_...
JWT BearerUser sessions, web apps, short-lived accessAuthorization: Bearer eyJ...

API Key authentication

API keys are the simplest way to authenticate. Include your key in the X-API-Key header:

import os
from engramma_cloud import EngrammaClient

client = EngrammaClient(api_key=os.environ['ENGRAMMA_API_KEY'])

# The SDK automatically sends X-API-Key header

Key format

PrefixEnvironmentUse
nx_live_ProductionReal data, billing applies
nx_test_DevelopmentSandbox, no billing

Keys are 48 characters total (8-char prefix + 40-char random string).

Key scopes

Each API key can be restricted to specific operations:

ScopeAllows
memory:readRetrieve, recall, explain, search
memory:writeStore, batch store, forget, tag, link
memory:adminConsolidation, diagnostics, config
org:readView organization details and members
org:writeManage members, create keys
billing:readView plans and invoices
billing:writeChange plan, manage subscription
Tip

Follow the principle of least privilege. Give each key only the scopes it needs.


JWT Bearer authentication

For user-facing applications, use JWT tokens obtained via the login flow:

Login flow

from engramma_cloud import EngrammaClient

client = EngrammaClient()

# Step 1: Login to get tokens
tokens = client.auth.login(
    email='[email protected]',
    password='your-password'
)
print(tokens.access_token)   # JWT, expires in 15 minutes
print(tokens.refresh_token)  # Long-lived, expires in 7 days

# Step 2: Use access token for requests
client = EngrammaClient(access_token=tokens.access_token)

Token lifecycle

TokenLifetimeStorage recommendation
Access token15 minutesMemory only (never persist)
Refresh token7 daysSecure HTTP-only cookie or encrypted storage

Refresh flow

When the access token expires, use the refresh token to get a new one without re-authenticating:

# Access token expired — refresh it
new_tokens = client.auth.refresh(refresh_token=tokens.refresh_token)
print(new_tokens.access_token)  # new JWT, 15 more minutes

# Update the client
client = EngrammaClient(access_token=new_tokens.access_token)
Info

The SDKs handle token refresh automatically when configured with both tokens. You only need to handle this manually with raw HTTP.


Multi-Factor Authentication (MFA)

MFA adds a second layer of protection using TOTP (Time-based One-Time Password) apps like Google Authenticator or Authy.

Enable MFA

# Step 1: Initiate MFA setup — returns a QR code URL
setup = client.auth.mfa_enable()
print(setup.qr_code_url)     # Show this QR to the user
print(setup.secret)          # Manual entry backup
print(setup.backup_codes)    # One-time recovery codes

# Step 2: Verify setup with a code from the authenticator app
client.auth.mfa_verify(code='123456')
print('MFA is now active')

Login with MFA

When MFA is enabled, the login flow requires a second step:

# Login returns a partial token that requires MFA verification
partial = client.auth.login(
    email='[email protected]',
    password='your-password'
)
# partial.mfa_required == True

# Complete login with TOTP code
tokens = client.auth.mfa_verify(
    mfa_token=partial.mfa_token,
    code='123456'
)
# Now you have full access_token and refresh_token

Backup codes

Store backup codes securely when MFA is first enabled. Each code can only be used once:

# Use a backup code if you lose access to your authenticator
tokens = client.auth.mfa_verify(
    mfa_token=partial.mfa_token,
    code='ABCD-EFGH-IJKL'  # one of the backup codes
)
# The used backup code is immediately invalidated

Choosing the right method

ScenarioRecommendedWhy
Backend service / cron jobAPI KeyLong-lived, no refresh needed
CI/CD pipelineAPI Key (test)nx_test_ key, limited scope
Web app (user sessions)JWT BearerShort-lived, per-user access
Mobile appJWT BearerRefresh flow for seamless UX
Admin dashboardJWT + MFAMaximum security for sensitive ops
Third-party integrationScoped API KeyMinimal permissions

Security checklist

  1. Never commit keys — Use environment variables or a secrets manager
  2. Use test keys for developmentnx_test_ keys have no billing impact
  3. Scope keys minimally — Only grant permissions the service actually needs
  4. Enable MFA — Required for organization admins, recommended for all users
  5. Rotate keys regularly — Use the rotation endpoint with a grace period
  6. Monitor the audit log — Watch for failed logins and unexpected key usage

Next steps