Overview
@grantex/gateway is a standalone reverse-proxy that enforces Grantex grant tokens in front of any upstream API. Define routes and required scopes in a YAML config — no code required.
Quick Start
1. Creategateway.yaml:
How It Works
- Route matching — finds the first route matching the request method + path
- Token verification — extracts Bearer token and verifies offline via JWKS
- Scope checking — ensures the grant includes all required scopes
- Proxy — strips Authorization header, adds upstream headers +
X-Grantex-*context, forwards to upstream - Response — returns the upstream response to the client
YAML Config Reference
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
upstream | string | Yes | — | Base URL of the upstream API |
jwksUri | string | Yes | — | JWKS endpoint for offline verification |
port | number | No | 8080 | Listen port |
upstreamHeaders | object | No | — | Headers added to every upstream request |
grantexApiKey | string | No | — | API key for audit logging |
routes | array | Yes | — | Route definitions |
Route Definition
| Field | Type | Description |
|---|---|---|
path | string | URL pattern. * matches one segment, ** matches any depth |
methods | string[] | HTTP methods (GET, POST, PUT, PATCH, DELETE) |
requiredScopes | string[] | Scopes that must be present in the grant |
Path Matching
| Pattern | Matches | Does Not Match |
|---|---|---|
/users/* | /users/123 | /users/123/profile |
/calendar/** | /calendar/events, /calendar/events/123/attendees | /api/calendar |
/health | /health | /health/check |
Context Headers
The gateway adds these headers to every upstream request:| Header | Value |
|---|---|
X-Grantex-Principal | Principal ID (end-user) |
X-Grantex-Agent | Agent DID |
X-Grantex-GrantId | Grant ID |
Error Responses
All errors return JSON witherror and message fields:
| Status | Error Code | When |
|---|---|---|
| 404 | ROUTE_NOT_FOUND | No route matches the request |
| 401 | TOKEN_MISSING | No Bearer token in Authorization header |
| 401 | TOKEN_INVALID | Token signature verification failed |
| 401 | TOKEN_EXPIRED | Token has expired |
| 403 | SCOPE_INSUFFICIENT | Grant doesn’t include required scopes |
| 502 | UPSTREAM_ERROR | Upstream API is unreachable |