API Key Best Practices
intermediateSecure your Engramma API keys: format, rotation, scoping, environment separation, and secret management.
Key format
Engramma API keys follow a predictable format that makes them easy to identify and manage:
nx_live_a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4
└─────┘└──────────────────────────────────────────────────────┘
prefix 40-char random string
| Prefix | Environment | Billing | Data |
|---|---|---|---|
nx_live_ | Production | Charges apply | Real user data |
nx_test_ | Development | Free | Sandbox (reset weekly) |
Environment separation
Never use production keys in development. The two-prefix system makes this easy to enforce:
# Development — uses sandbox data, no charges
ENGRAMMA_API_KEY=nx_test_dev1234567890abcdefghijklmnopqrstuvwxyz12Never use nx_live_ keys in CI/CD test runs. A failing test could store garbage in your production memory space or trigger billing.
Scoping
Every API key should have the minimum permissions required for its use case:
| Use case | Recommended scopes |
|---|---|
| Read-only client | memory:read |
| Write-only ingestion | memory:write |
| Full application backend | memory:read, memory:write |
| Admin tooling | memory:admin, org:read, org:write |
| Billing dashboard | billing:read |
| CI/CD testing | memory:read, memory:write |
| Monitoring webhook | memory:read, org:read |
# Create a scoped key via the API
new_key = client.organizations.create_key(
org_id='org_abc123',
name='Read-Only Analytics',
scopes=['memory:read']
)
print(f'Key: {new_key.key}') # only shown onceRotation
Rotate keys regularly to limit the blast radius of a leak. The rotation endpoint provides a grace period so you can update services without downtime:
# Rotate with a 24-hour grace period
rotation = client.organizations.rotate_key(
org_id='org_abc123',
key_id='key_old01',
grace_period_hours=24
)
print(f'New key: {rotation.new_key}') # start using this
print(f'Old key valid until: {rotation.old_key_expires_at}')
# During the grace period, BOTH keys work
# After it expires, only the new key worksRotation schedule
| Risk level | Rotation frequency | Grace period |
|---|---|---|
| High (public-facing) | Every 30 days | 24 hours |
| Medium (internal services) | Every 90 days | 48 hours |
| Low (admin, rarely used) | Every 180 days | 72 hours |
Secret management
Where to store keys
| Method | Security | Best for |
|---|---|---|
| Environment variables | Good | Local dev, containers |
| Cloud secrets manager | Best | Production (AWS Secrets Manager, GCP Secret Manager, Azure Key Vault) |
.env file (gitignored) | Acceptable | Local development only |
| Hardcoded in source | Never | — |
Gitignore rules
Always ensure your .gitignore excludes secret files:
# Environment files
.env
.env.local
.env.production
.env.*.local
# Secret files
*.pem
*.key
credentials.json
secrets.yamlPre-commit hook
Prevent accidental key commits with a pre-commit hook:
#!/bin/bash
# .git/hooks/pre-commit
# Block commits containing Engramma API keys
if git diff --cached --diff-filter=ACMR | grep -qE 'nx_(live|test)_[a-z0-9]{40}'; then
echo 'ERROR: Engramma API key detected in staged changes.'
echo 'Remove the key and use an environment variable instead.'
exit 1
fiKey lifecycle
Create → Scope → Deploy → Monitor → Rotate → Revoke
- Create — Generate a new key with a descriptive name
- Scope — Assign minimum required permissions
- Deploy — Store in secrets manager, reference via environment variable
- Monitor — Check the audit log for unusual usage
- Rotate — Replace periodically with grace period
- Revoke — Immediately invalidate compromised keys
Emergency revocation
If a key is leaked, revoke it immediately:
# Immediate revocation — no grace period
client.organizations.revoke_key(
org_id='org_abc123',
key_id='key_compromised'
)
# Key stops working instantlyAfter revoking a compromised key, review the audit log to check what actions were taken with it. Consider requesting an erasure if sensitive data may have been accessed.
Common mistakes
| Mistake | Risk | Fix |
|---|---|---|
| Hardcoding keys in source | Exposed in git history | Use environment variables |
Using nx_live_ in tests | Pollutes production data | Always use nx_test_ for dev/CI |
| Single key for everything | Over-privileged access | Create scoped keys per service |
| Never rotating | Longer exposure window | Set a rotation schedule |
| Sharing keys via Slack/email | Interceptable | Use a secrets manager |
| No monitoring | Undetected abuse | Review audit log weekly |
Next steps
- Authentication — Full auth flow with JWT and MFA
- Your Data Rights — GDPR export and erasure
- Organizations API — Key management endpoints
- Security API — Audit log and sessions