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.2.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

Quick Start

Express

Fastify

Manual Token Verification

Core API

verifyToken(token, apiKey)

Verifies and decodes a Gately JWT token.

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