NestJS API
Protect a NestJS API with Authgear using the @authgear/nestjs SDK
Protect a NestJS API with Authgear using the @authgear/nestjs SDK. The SDK validates Authgear JWT access tokens offline (via OIDC discovery and JWKS) and gives you a NestJS module, an authentication guard, and decorators — so protecting a route takes just a few lines.
A complete example application is available at authgear/authgear-example-nestjs.
What you will build:
A public route (
GET /health) that needs no tokenA protected route (
GET /me) that returns the authenticated user's claims
Setting Up Your Application in Authgear
Step 1: Enable JWT access tokens
The SDK validates JWT access tokens offline, so the application that issues the tokens your API will accept must issue JWTs.
Sign in to the Authgear Portal
Select your Project, then go to Applications and open the application your client uses to log in (or create one — e.g. a Single Page Application)
Under the Access Token section, turn on Issue JWT as access token
Note down your Endpoint (e.g.
https://your-project.authgear.cloud) and Client ID — you will need these shortlyClick Save
Without Issue JWT as access token, Authgear issues opaque access tokens, which this SDK cannot validate offline. Requests would be rejected with 401.
Building Your NestJS Application
Step 1: Create a NestJS Project
Step 2: Install the Authgear SDK
This also installs @nestjs/config, which we use to read configuration from the environment:
Step 3: Configure Environment Variables
Create a .env file in the project root:
Step 4: Register the Authgear Module
Register AuthgearModule in your root module. Setting global: true registers the guard as a global guard, so every route is protected by default unless explicitly marked public.
Step 5: Protect Your Routes
With the guard registered globally, mark public routes with @Public(). Read the authenticated user with the @CurrentUser() parameter decorator.
AuthgearClaims exposes the common claims (sub, iss, aud, clientID, isVerified, isAnonymous, canReauthenticate) plus the full decoded payload as raw for any custom claims.
Running the Application
The API listens on http://localhost:3000.
Testing the Integration
The public route works without a token:
The protected route is rejected without a valid token:
To call the protected route, send an Authgear access token as a Bearer token:
Module Options
forRoot() and the object returned by the forRootAsync() factory accept:
endpoint
string
✓
—
Authgear project endpoint. Used for OIDC discovery and JWKS.
clientID
string
—
If set, the verifier also asserts the token's client_id claim.
global
boolean
false
Register the guard as a global APP_GUARD so all routes are protected.
jwksCacheMaxAge
number
—
JWKS cache max age in milliseconds.
clockToleranceSeconds
number
0
Leeway in seconds for exp/iat checks.
You can also inject AuthgearTokenService to verify a token outside of the guard.
Next Steps
JWT access token reference — the full list of claims
Validate JWT in your backend — the framework-agnostic approach
Last updated
Was this helpful?