Client SDK

Client Setup

Create and configure the Monospace SDK client.

Create a Client

Import createClient from the generated output and pass your connection details. The generated factory is pre-typed with your Schema, so every collection accessor is fully typed.

client.ts
import { createClient } from './generated/monospace';

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

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

Connection

OptionTypeDefaultDescription
urlstringBase URL of your Monospace instance (required)
projectstringProject identifier (required)
apiKeystringundefinedAPI key for authentication

Advanced

OptionTypeDefaultDescription
unwrapEnvelopebooleantrueStrip the { data } envelope from responses. See Response Envelope
strictNullbooleantrueApply | null to all fields for conditional permission safety. See Null Strictness
httpHttpClientFactoryBuilt-in clientCustom HTTP client factory for custom fetch implementations

Custom HTTP Client

The http option accepts a factory function (config: HttpClientConfig) => HttpClient. The returned object must implement get, post, patch, and delete. Use this for request logging, custom retry logic, or token refresh.

const client = createClient({
  url: 'https://example.monospace.io',
  project: 'blog',
  apiKey: 'YOUR_API_KEY',
  http: (config) => ({
    get: (path, opts) => { /* your implementation */ },
    post: (path, opts) => { /* your implementation */ },
    patch: (path, opts) => { /* your implementation */ },
    delete: (path, opts) => { /* your implementation */ },
  }),
});

Configure from Environment Variables

Read connection details from environment variables to avoid hardcoding secrets.

client.ts
import process from 'node:process';
import { createClient } from './generated/monospace';

import 'dotenv/config';

export const client = createClient({
  url: 'http://localhost:3000',
  project: 'dev',
  apiKey: process.env.MONOSPACE_API_KEY!,
});
.env
MONOSPACE_API_KEY=your-api-key-here
Never commit API keys to source control. Use .env files, CI secrets, or a secrets manager.

Customize Response Behavior

The SDK provides two response modifiers that can be set at the client level or overridden per request:

  • unwrapEnvelope — Controls whether the SDK strips the { data } wrapper from API responses. Default: true (unwrapped). Set to false to preserve the envelope for forward compatibility with future metadata fields.
  • strictNull — Controls whether all fields include | null in their types to account for conditional field-level permissions. Default: true (strict). Set to false when your auth context has full field access.

Both options support per-request overrides via a second argument to any CRUD method:

const response = await client.Articles.readMany(
  { fields: ['id', 'title'] },
  { unwrapEnvelope: false, strictNull: false },
);

For detailed examples and explanations, see Advanced.


Next Steps

Copyright © 2026