@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.
Install
Install the SDK together with@archie/js-sdk, which is a required peer dependency.
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.authfor JWT verification and admin user operations.archie.graphqlfor service-role GraphQL queries and mutations.archie.restfor service-role REST requests.archie.notificationsfor emitting notifications from the server.
Verify JWTs
Usearchie.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.
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 }.
Create a user
Create a user
List users
List users
Update a user
Update a user
Block, unblock, and delete a user
Block, unblock, and delete a user
Service-role GraphQL
Run GraphQL operations with admin-level permissions.(gql, variables?, options?).
Impersonate a user
PassimpersonateUserId 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.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.
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 theAuthorization header.
- Express
- Fastify
- Lambda
Install Express, then import from The middleware augments Express types, so
@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.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 }, whereerrorisArchieError | null. - Verify operations return
{ user, error }, whereerrorisAuthError | null. - REST operations throw
ArchieErroron 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.- HTTP status codes
429,500,502,503, and504. - Network errors, such as DNS failures and refused connections.
- Timeout errors.
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 customfetch for unit tests with a mocked fetch, HTTP proxies or agents (for example undici with a proxy), or transport-level logging.
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.
Security best practices
- Keep
apiKeyin 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
requiredRolesorrequireRoleto restrict sensitive routes, andrequireEmailVerifiedwhen you need verified accounts. - Use
required: trueon the middleware for routes that must be authenticated. - Do not log API keys or raw tokens through the
loggeror a customfetch.
TypeScript
The SDK ships with types. Import them withimport type.
@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.