Skip to main content
@archie/node-sdk is the server SDK for Archie. Use it in trusted backend code to verify JWTs, manage users as an admin, run service-role GraphQL and REST requests, and protect routes with framework middleware. It is built for backend developers who run their own server or serverless functions next to an Archie project. For browser code, use the @archie/js-sdk package instead.
The Node SDK authenticates with a service-role API key. This key has admin-level access. Use it only in server-side code, load it from environment variables, and never ship it to a browser, mobile app, or public repository.

Install

Install the SDK together with @archie/js-sdk, which is a required peer dependency.
Framework integrations are optional. Install express or fastify only if you use them. The SDK requires Node.js 18.0.0 or later.

Quick start

Create an admin client with your project ID and service-role API key.

Configuration

createAdminClient(options) returns an ArchieAdminClient. Only projectId and apiKey are required. The client exposes these modules:
  • archie.auth for JWT verification and admin user operations.
  • archie.graphql for service-role GraphQL queries and mutations.
  • archie.rest for service-role REST requests.
  • archie.notifications for emitting notifications from the server.

Verify JWTs

Use archie.auth.verifyToken(token, options?) to verify JWTs issued by Archie. The SDK checks signatures against the project’s JWKS (JSON Web Key Set). It caches keys for 5 minutes and shares one fetch between concurrent calls.
The token can include a Bearer prefix. The SDK strips it for you. The call resolves to a VerifyTokenResult:

Manage users as an admin

Admin operations use service-role authentication and return { data, error }.

Service-role GraphQL

Run GraphQL operations with admin-level permissions.
Both methods use the signature (gql, variables?, options?).

Impersonate a user

Pass impersonateUserId to run an operation as a specific user while keeping service-role access. The SDK adds the x-impersonate-user header to the request.

Service-role REST

Call REST endpoints directly with admin credentials.
REST methods throw an ArchieError on non-2xx responses. The error code includes the HTTP status.
Endpoints that return 204 No Content or Content-Length: 0 resolve with null data instead of parsing an empty body.

Emit notifications

archie.notifications.emit() dispatches a notification through the backend emitNotification mutation. eventKey, target, and sourceService are required.
Admin emit() always returns { data, error } and never throws. This differs from the browser-side @archie/js-sdk notification methods (list, getUnreadCount, markAsRead, markAllAsRead), which throw ArchieError on failure.

Middleware and framework integrations

Each integration lives in its own subpath export and verifies the JWT in the Authorization header.
Install Express, then import from @archie/node-sdk/express.
createArchieMiddleware(client, options?) verifies the JWT and populates req.archieUser.
requireRole(...roles) checks that req.archieUser has at least one of the given roles and returns 403 if not. Use it after createArchieMiddleware.
The middleware augments Express types, so req.archieUser is typed as TokenUser | undefined.

Error handling

Each part of the SDK reports errors in a consistent way:
  • Auth operations return { data, error }, where error is ArchieError | null.
  • Verify operations return { user, error }, where error is AuthError | null.
  • REST operations throw ArchieError on non-2xx responses.
  • GraphQL operations return { data, error } with structured GraphQL errors.

Retries and timeouts

All HTTP requests go through one engine with configurable retry and timeout.
The SDK retries on these conditions:
  • HTTP status codes 429, 500, 502, 503, and 504.
  • Network errors, such as DNS failures and refused connections.
  • Timeout errors.
Backoff is exponential with jitter: delay × 2^attempt + random jitter, capped at 30 seconds. If a 429 response includes a Retry-After header, the SDK respects it. Requests time out after 30 seconds by default. Set timeout: 0 to disable timeouts. A timed-out request throws an ArchieError with code: 'TIMEOUT' and status: 408.

Health check

archie.ping() sends a GET /health request. It returns true if the server responds with 2xx within the configured timeout, and false on any error.

Custom fetch and logging

Pass a custom fetch for unit tests with a mocked fetch, HTTP proxies or agents (for example undici with a proxy), or transport-level logging.
Pass a logger to observe request lifecycle events. Any logger works, including pino, winston, and console. All methods are optional, so implement only the levels you need.
The SDK logs these events:

Security best practices

  • Keep apiKey in server-side environment variables or a secrets manager.
  • Never import the admin client in code that runs in the browser or in a mobile app.
  • Use requiredRoles or requireRole to restrict sensitive routes, and requireEmailVerified when you need verified accounts.
  • Use required: true on the middleware for routes that must be authenticated.
  • Do not log API keys or raw tokens through the logger or a custom fetch.

TypeScript

The SDK ships with types. Import them with import type.
The SDK also re-exports these types from @archie/js-sdk: User, Session, GraphQLResponse, ArchieError, AuthError, GraphQLError, and NetworkError. The ArchieAdminClient class and the module classes AdminAuthModule, AdminGraphQLModule, and AdminRestModule are exported for instanceof checks, advanced typing, or mocking. new ArchieAdminClient({ projectId: '...', apiKey: '...' }) is equivalent to createAdminClient.

Resources

@archie/node-sdk on npm

Package page, versions, and install instructions.

Backend overview

Learn about the Backend Console and the APIs your project exposes.