Skip to main content
The @gately/nodejs SDK provides JWT-based authentication for Node.js backends with first-class support for Express and Fastify.
Version: @gately/nodejs@2.4.0
Features: Token verification, Express middleware, Fastify plugin, token caching, TypeScript support

Installation

Or install a specific version:
Get your API key: Dashboard → Settings → API Keys
Use a secret key (gately_sk_...) and keep it on your server. Token verification is rejected for public keys (gately_pk_...) and project IDs.

Quick Start

Express

Fastify

Manual Token Verification

Core API

verifyToken(token, apiKey, options?)

Verifies a Gately session token and returns its claims. Verification happens on Gately’s servers (POST /api/v1/auth/verify): the signature, expiry, and project are all checked, so a token is only accepted if Gately issued it to a member of the project that owns apiKey. Results are cached in memory for up to 5 minutes (never past the token’s expiry), so most requests don’t make a network call.
Versions before 2.4.0 decoded the token locally without checking its signature, and rejected real session tokens. Upgrade to 2.4.0 or later.

validateToken(token, apiKey)

Alias for verifyToken.

getAuthorizationHeader(authHeader)

Extracts the JWT from an Authorization header.

Express Integration

Global Middleware

Per-Route: requireAuth

Per-Route: optionalAuth

Attaches user to req.auth if token is present, but doesn’t block if missing:

Per-Route: withAuth

Attach a custom callback after verification:

protect

Shorthand route-level protection:

Fastify Integration

Plugin (Global)

Route-Level Protection

Request Object

Middleware attaches an auth object to every authenticated request:

Utility Functions

isTokenExpired

getTokenExpiry

Token Cache

Tokens are cached in memory to avoid re-decoding on every request:

Error Handling

Role-Based Access

TypeScript

Environment Variables

Troubleshooting

  • Confirm the token was issued by Gately and hasn’t expired
  • Ensure apiKey matches the project the token was issued for
  • Check the Authorization header format: Bearer <token>
Middleware must be registered before route handlers:
Ensure the middleware ran successfully. Check that:
  • A valid Authorization: Bearer <token> header is sent
  • The route is not in excludePaths
  • optional is not set to true (which silently skips bad tokens)

Next Steps

React SDK

Client-side authentication

Next.js SDK

Full-stack Next.js auth
Support: support@usegately.com · Slack Community