This guide shows how to install the MonoCloud Backend Node SDK and configure your Express API with MonoCloud.
By the end of this guide, you will have:
If you already have a MonoCloud account and API, skip ahead to Install the SDK.
If you don’t have an account yet, sign up at: https://www.monocloud.com
In the MonoCloud Dashboard:
https://api.example.com) — this uniquely identifies your APIread, write)Each API represents a single resource server secured by MonoCloud.
Install the MonoCloud Backend Node SDK using your package manager:
npm install @monocloud/backend-node
Node.js 18 or later is required.
The SDK reads configuration from environment variables prefixed with MONOCLOUD_BACKEND_.
Create a .env file in your project root:
MONOCLOUD_BACKEND_TENANT_DOMAIN=https://<your-domain>
MONOCLOUD_BACKEND_AUDIENCE=https://<your-api-audience>
| Environment variable | Where to find the value in MonoCloud |
|---|---|
MONOCLOUD_BACKEND_TENANT_DOMAIN | Domain from your tenant or API settings |
MONOCLOUD_BACKEND_AUDIENCE | Audience from the API settings |
Only required when using token introspection for opaque tokens:
MONOCLOUD_BACKEND_CLIENT_ID=<your-client-id>
MONOCLOUD_BACKEND_CLIENT_SECRET=<your-client-secret>
Once environment variables are set, use protectApi() to protect your routes:
import "dotenv/config";
import express from "express";
import { protectApi } from "@monocloud/backend-node/express";
const app = express();
const protect = protectApi();
app.use(express.json());
app.use(protect());
app.get("/api/data", (req, res) => {
res.json({ message: "Protected data" });
});
app.listen(3000, () => {
console.log("Server running on http://localhost:3000");
});
protectApi() automatically reads configuration from environment variables. No additional setup is required.
| Environment variable | Description | Required |
|---|---|---|
MONOCLOUD_BACKEND_TENANT_DOMAIN | Your MonoCloud tenant domain URL | Yes |
MONOCLOUD_BACKEND_AUDIENCE | The expected audience for token validation | Yes |
MONOCLOUD_BACKEND_CLIENT_ID | Client ID (for token introspection) | No |
MONOCLOUD_BACKEND_CLIENT_SECRET | Client secret (for token introspection) | No |
MONOCLOUD_BACKEND_CLIENT_AUTH_METHOD | Client authentication method | No |
MONOCLOUD_BACKEND_CLOCK_SKEW | Allowed clock drift in seconds | No |
MONOCLOUD_BACKEND_CLOCK_TOLERANCE | Time tolerance for claim validation in seconds | No |
MONOCLOUD_BACKEND_INTROSPECT_JWT_TOKENS | When true, JWT tokens are also introspected instead of only being validated locally | No |
MONOCLOUD_BACKEND_GROUPS_CLAIM | Token claim name containing group memberships | No |
MONOCLOUD_BACKEND_GROUPS_MATCH_ALL | When true, requires all specified groups to be present | No |
MONOCLOUD_BACKEND_JWKS_CACHE_DURATION | JWKS cache duration in seconds | No |
MONOCLOUD_BACKEND_METADATA_CACHE_DURATION | OIDC metadata cache duration in seconds | No |
Explore advanced protection patterns: