Sign in

Install and set up the MonoCloud Backend Node SDK

This guide shows how to install the MonoCloud Backend Node SDK and configure your Fastify API with MonoCloud.

By the end of this guide, you will have:

  • Your API configured in the MonoCloud Dashboard
  • The SDK installed in your Fastify project
  • Environment variables configured for token validation

Sign up and configure MonoCloud

If you already have a MonoCloud account and API, skip ahead to Install the SDK.

Create a MonoCloud account

If you don’t have an account yet, sign up at: https://www.monocloud.com

Create an API

In the MonoCloud Dashboard:

  1. Click Add API
  2. Set the Audience (for example https://api.example.com) — this uniquely identifies your API
  3. Add any scopes your API requires (for example read, write)

Each API represents a single resource server secured by MonoCloud.

Install the SDK

Install the MonoCloud Backend Node SDK using your package manager:

Terminal
npm install @monocloud/backend-node

Node.js 18 or later is required.

Configure environment variables

The SDK reads configuration from environment variables prefixed with MONOCLOUD_BACKEND_.

Create a .env file in your project root:

.env
MONOCLOUD_BACKEND_TENANT_DOMAIN=https://<your-domain>
MONOCLOUD_BACKEND_AUDIENCE=https://<your-api-audience>

Where to find these values

Environment variableWhere to find the value in MonoCloud
MONOCLOUD_BACKEND_TENANT_DOMAINDomain from your tenant or API settings
MONOCLOUD_BACKEND_AUDIENCEAudience from the API settings

Additional environment variables

Only required when using token introspection for opaque tokens:

.env
MONOCLOUD_BACKEND_CLIENT_ID=<your-client-id>
MONOCLOUD_BACKEND_CLIENT_SECRET=<your-client-secret>

Protect your API

Once environment variables are set, use protectApi() to protect your routes:

src/server.ts
import "dotenv/config";
import Fastify from "fastify";
import { protectApi } from "@monocloud/backend-node/fastify";

const app = Fastify();
const protect = protectApi();

app.addHook("onRequest", protect());

app.get("/api/data", async () => {
  return { message: "Protected data" };
});

app.listen({ port: 3000 }, (err) => {
  if (err) throw err;
  console.log("Server running on http://localhost:3000");
});

protectApi() automatically reads configuration from environment variables. No additional setup is required.

Environment variable reference

Environment variableDescriptionRequired
MONOCLOUD_BACKEND_TENANT_DOMAINYour MonoCloud tenant domain URLYes
MONOCLOUD_BACKEND_AUDIENCEThe expected audience for token validationYes
MONOCLOUD_BACKEND_CLIENT_IDClient ID (for token introspection)No
MONOCLOUD_BACKEND_CLIENT_SECRETClient secret (for token introspection)No
MONOCLOUD_BACKEND_CLIENT_AUTH_METHODClient authentication methodNo
MONOCLOUD_BACKEND_CLOCK_SKEWAllowed clock drift in secondsNo
MONOCLOUD_BACKEND_CLOCK_TOLERANCETime tolerance for claim validation in secondsNo
MONOCLOUD_BACKEND_INTROSPECT_JWT_TOKENSWhen true, JWT tokens are also introspected instead of only being validated locallyNo
MONOCLOUD_BACKEND_GROUPS_CLAIMToken claim name containing group membershipsNo
MONOCLOUD_BACKEND_GROUPS_MATCH_ALLWhen true, requires all specified groups to be presentNo
MONOCLOUD_BACKEND_JWKS_CACHE_DURATIONJWKS cache duration in secondsNo
MONOCLOUD_BACKEND_METADATA_CACHE_DURATIONOIDC metadata cache duration in secondsNo
© 2024 MonoCloud. All rights reserved.