Client SDK

Installation

Install the SDK, configure your project, and generate types.

Prerequisites

Check these requirements before installing the SDK:

RequirementDetails
Node.jsLTS recommended
Monospace instanceRunning and accessible
TypeScriptRecommended — 5.0+ with strict: true

Generated types, field autocomplete, and compile-time schema checks require TypeScript. The client runs without it — see Use the SDK from JavaScript.

With TypeScript, enable strict mode in your tsconfig.json:

tsconfig.json
{
  "compilerOptions": {
    "strict": true
  }
}
Warning
The generated types rely on TypeScript's strict null checks. Without strict: true, nullable fields lose their | null annotations.

Install the Packages

Add @monospace/sdk to your project, and the Monospace CLI, @monospace/cli, as a development dependency. The CLI generates the types.

npm install @monospace/sdk
npm install -D @monospace/cli

Add the Monospace SDK to Your Project

Run sdk init to create your config file interactively.

npx @monospace/cli sdk init

The command walks through these steps:

  1. Prompt for your Monospace instance URL
  2. Prompt for the workspace whose schema to read
  3. Ask for an output directory (default: ./src/generated/monospace)
  4. Write a monospace.config.ts file

Pass --url, --workspace, and --dir to skip the matching prompts.


Configure Your Project

The SDK generator reads your Monospace schema at build time and produces a typed TypeScript client. Regenerate the client after schema changes to keep its types in sync.

Create a monospace.config.ts (or monospace.config.js) file at your project root. It specifies your Monospace instance URL and the workspace whose schema you want to read.

The CLI loads either extension, so a .ts config works in a JavaScript project.

Remote Config

Set your Monospace instance URL and workspace identifier:

monospace.config.ts
import { defineConfig } from '@monospace/sdk/config'

export default defineConfig({
  url: 'https://example.monospace.io',
  workspace: 'blog',
  output: './src/generated/monospace',
})

Local Config

Generate types from a local OpenAPI spec file instead:

monospace.config.ts
import { defineConfig } from '@monospace/sdk/config'

export default defineConfig({
  input: './local-schema.json',
  output: './src/generated/monospace',
})

Config Reference

OptionTypeDescription
outputstringOutput directory for generated types. Required.
urlstringMonospace instance URL. Required for remote config unless --url or MONOSPACE_URL supplies it.
workspacestringWorkspace identifier whose schema to read. Required for remote config unless --workspace or MONOSPACE_WORKSPACE supplies it.
inputstringPath to a local OpenAPI spec file. Required for local config.
outputOptionsOutputOptionsControls the generated output. Optional.

A remote config uses url and workspace; a local config uses input. Use one configuration or the other.

For a remote config, the CLI resolves the instance URL and the workspace separately, each from the first of: the --url or --workspace flag, the MONOSPACE_URL or MONOSPACE_WORKSPACE environment variable, then the config file.

The CLI rejects a config file that sets apiKey, api_key, password, secret, or token. Supply credentials as described below.

outputOptions controls the generated output:

OptionTypeDefaultDescription
strictUuidbooleantrueUse template literal type for format: 'uuid' fields (Uuid type)
Note
defineConfig checks your config's types and returns it unchanged.

Authenticate for Generation

Remote configs require authentication to fetch your schema. The CLI resolves credentials in this order:

  1. --api-key <key> flag on the CLI command
  2. MONOSPACE_API_KEY environment variable (.env files are loaded automatically)
  3. OS keychain (stored via the login command)

Using an Environment Variable

Set the API key in your project's .env file:

.env
MONOSPACE_API_KEY=your-api-key-here

Using the Login Command

Run login to authenticate with an API key or email and password:

npx @monospace/cli login

The command prompts for credentials and stores them in your OS keychain.

To remove stored credentials:

npx @monospace/cli logout
Note
The API key used for generation only needs read access to your schema. Create a dedicated service account key with minimal permissions under Settings > Access > API Keys in Studio.

Generate Types from Your Schema

Run the generator to read your schema and produce a typed client:

npx @monospace/cli sdk generate

This produces a typed client you can import:

your-project/
├── src/
│   └── generated/
│       └── monospace/
│           └── index.ts
├── monospace.config.ts
├── package.json
└── tsconfig.json

Import the client factory from the generated output. It includes your schema's types, so you do not need generic parameters:

src/client.ts
import { createClient } from './generated/monospace';
Tip
Add the generation command to your package.json scripts:
package.json
{
  "scripts": {
    "monospace:generate": "monospace sdk generate"
  }
}

Check Your Setup

Show which instance and workspace the current directory targets, which credential the CLI uses, and who it signs you in as:

npx @monospace/cli whoami

When a credential is found, whoami contacts the instance to confirm the identity. Add --local to report the resolved target and credential without contacting the instance.


CLI Reference

@monospace/cli installs a binary named monospace. Call it directly inside a package.json script:

CommandDescriptionOptions
sdk initCreate a monospace.config.ts in the working directory--url <url>, --workspace <name>, --dir <dir>, -c, --config <path>, --yes
sdk generateGenerate TypeScript types from your schema--url <url>, --workspace <name>, --api-key <key>, -c, --config <path>
loginStore a credential for a Monospace instance--url <url>, --api-key-stdin
logoutRemove a stored credential; for a session, first try to revoke it on the instance--url <url>
whoamiShow the targeted instance and the credential that answers for it--url <url>, --workspace <name>, --api-key <key>, --local

Every command also accepts --json to print a single JSON result on stdout, and --no-input to fail instead of prompting. See the CLI reference for every command, flag, and error.

Warning
Do not run npx monospace <command>. monospace is an unrelated package on the npm registry, and npx fetches it when the CLI is not installed locally. Use npx @monospace/cli <command>.

Regenerate After Schema Changes

When you add, modify, or remove collections or fields in Studio, re-run the generator to keep your types in sync.

npx @monospace/cli sdk generate

The generator overwrites the output file on each run, so manual edits are lost.

Note
Commit the generated output so teammates can use the typed client without access to your Monospace instance at build time.

Use the SDK from JavaScript

The generator emits a TypeScript file. JavaScript projects skip generation and import createClient from the package instead.

client.js
import { createClient } from '@monospace/sdk';

const client = createClient({
  url: 'https://example.monospace.io',
  workspace: 'blog',
  apiKey: 'YOUR_API_KEY',
});

const articles = await client.Articles.readMany({
  fields: ['id', 'title', 'status'],
});

Collections and methods resolve at runtime. Filters, field selection, readFirst, relational writes, and error classes behave the same as with the generated client. Autocomplete and compile-time type checks are unavailable.

Tip
Adding code generation later requires no call-site changes.

Next Steps

  • Client Setup — create and configure the client
  • Type System — understand how schema types map to TypeScript
  • CLI — every monospace command and flag
  • API Overview — understand the underlying REST API

MonospaceThe governed API layer for every app, person, and agent.

Copyright © 2026 Monospace Inc.