Authentication

Last updated 27 days ago

j17 uses different auth methods depending on what you're doing. API keys for data operations, JWT for admin tasks. Each environment (prod/staging/test) has isolated credentials.

API Keys

Use API keys for instance data: writing events, reading aggregates, batch operations.

curl https://myapp.j17.dev/order/abc123 \
  -H "Authorization: Bearer j17_0_prod_abc123xyz"

Key format

j17_{version}_{environment}_{random}
Prefix Environment Works on
j17_0_prod_* Production *.j17.dev
j17_0_staging_* Staging *-staging.j17.dev
j17_0_test_* Test *-test.j17.dev

Keys are environment-scoped. A staging key won't work on production. This prevents accidents.

Creating keys

Via dashboard: Instance settings page, API Keys section.

Via API (requires JWT):

curl -X POST https://console.j17.dev/api/instances/$INSTANCE_ID/keys \
  -H "Authorization: Bearer $JWT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Production Backend",
    "scope": "write",
    "environment": "prod"
  }'

Key scopes

Scope Permissions
read GET aggregates, query events, read projections
write (read/write) All read permissions + POST events
deploy Deploy specs and projections — no data access
admin Read + write + deploy

Use read keys for frontend clients that only display data. Use write keys for backend services that need to write events. Write keys include full read access.

deploy is orthogonal to write: a key that writes events cannot redefine the schema, and a deploy key cannot write data. Use a deploy key in CI to push your spec and projections with just the API key — see Schema Deployment.

Rotating keys

curl -X POST https://console.j17.dev/api/keys/$KEY_ID/rotate \
  -H "Authorization: Bearer $JWT_TOKEN"

You can also schedule revocation with a grace period:

curl -X POST https://console.j17.dev/api/keys/$KEY_ID/schedule_revocation \
  -H "Authorization: Bearer $JWT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"revoke_at": "2024-02-01T00:00:00Z"}'

Storing keys

Don't commit keys to git. Use environment variables:

# .env
J17_API_KEY=j17_0_prod_abc123xyz
// Read from env, not hardcoded
const apiKey = process.env.J17_API_KEY;

Never expose write keys in frontend code. Browser clients should use read-only keys or go through your backend.

JWT Tokens

Use JWT for admin operations: managing API keys, deploying specs, configuring instances. All JWT-authenticated endpoints are on the console API (console.j17.dev).

curl https://console.j17.dev/api/instances \
  -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIs..."

Getting a token

Login with email/password:

curl -X POST https://console.j17.dev/api/login \
  -H "Content-Type: application/json" \
  -d '{
    "email": "admin@example.com",
    "password": "..."
  }'

Response:

{
  "token": "eyJhbGciOiJIUzI1NiIs...",
  "expires_at": 1705312800
}

Tokens expire after 24 hours. Refresh by logging in again.

Token contents

j17 JWTs contain:

{
  "sub": "user-uuid",
  "email": "admin@example.com",
  "role": "instance_admin",
  "instance_id": "instance-uuid",
  "iat": 1705226400,
  "exp": 1705312800
}

Don't decode JWTs client-side to make permission decisions. Always verify server-side.

Environment isolation

Each environment is completely isolated:

Environment URL Credentials
Production myapp.j17.dev j17_0_prod_* keys
Staging myapp-staging.j17.dev j17_0_staging_* keys
Test myapp-test.j17.dev j17_0_test_* keys

Data doesn't flow between environments. A user created in staging doesn't exist in production.

Testing against staging

# Always hit staging first
curl https://myapp-staging.j17.dev/user/abc123/was_created \
  -H "Authorization: Bearer $STAGING_KEY" \
  -d '{...}'

# Verify it works, then switch to production
curl https://myapp.j17.dev/user/abc123/was_created \
  -H "Authorization: Bearer $PROD_KEY" \
  -d '{...}'

Rate limits

Rate limits apply per instance, not per API key. All keys and environments for an instance share its budgets. Creating or rotating keys does not increase the allowance.

In each fixed one-minute window, write allowances are Free 600, Hobby 6,000, Pro 12,000, and Scale 60,000 requests. Reads have a separate budget 5x higher; deployments have a separate budget equal to writes. A per-IP safety net of 30,000 requests per minute on each worker is shared across these operations. See rate limits for the full table and retry guidance.

Platform admins and node-authenticated requests are exempt.

For example, a Free instance's write response can include:

RateLimit-Limit: 600
RateLimit-Remaining: 599
RateLimit-Scope: instance
RateLimit-Tier: free

The headers describe the instance or IP budget with the smaller remaining allowance. RateLimit-Scope is instance or ip; RateLimit-Tier accompanies instance budgets. RateLimit-Reset gives the reset time in Unix seconds. On a rate-limit 429, Retry-After gives the number of seconds to wait. Legacy X-RateLimit-* aliases remain available with identical values.

Error responses

Invalid key

{
  "ok": false,
  "error": "Invalid API key"
}

HTTP 401. Check the key is correct and not revoked.

Wrong environment

{
  "ok": false,
  "error": "Staging key cannot access production"
}

HTTP 403. You're using a staging key on production (or vice versa).

Insufficient scope

{
  "ok": false,
  "error": "Read-only key cannot write events"
}

HTTP 403. Get a write-scoped key or use a different endpoint.

Expired JWT

{
  "ok": false,
  "error": "Token expired"
}

HTTP 401. Log in again to get a fresh token.

Best practices

Use separate keys per service. Don't share one key across frontend, backend, and admin tools. If one leaks, you only rotate that one.

Read keys for frontend. If your browser code needs to fetch aggregates, use a read-only key. Better yet, proxy through your backend.

Monitor usage. Dashboard shows which keys are active. Disable unused keys.

Rotate regularly. Set a calendar reminder. Quarterly rotation catches leaked keys before they're exploited.

Never log keys. If you debug HTTP requests, redact the Authorization header.

See also

Can't find what you need? support@j17.app