Skip to content

Configuration

Basic Configuration

Basic Configurationtypescript
import SpaceInvoices from "@spaceinvoices/js-sdk";

const _sdk = new SpaceInvoices({
  accessToken: "YOUR_API_KEY",
  accountId: "acc_123", // Optional for multi-account user-token flows
});

Configuration Options

OptionTypeDescription
accessTokenstring | () => Promise<string>API key or async token provider
accountIdstring | nullOptional active account for multi-account user-token flows
basePathstringCustom API URL (default: https://eu.spaceinvoices.com)
onUnauthorized(response) => voidCallback for 401 responses

Use accountId when a browser app is using a user token and the user can switch between multiple accounts. API-key, embed, and single-account flows can leave it unset.

Dynamic Tokens

For applications that refresh tokens, provide an async function:

Dynamic Tokentypescript
const _sdk = new SpaceInvoices({
  accessToken: async () => {
    // Fetch token from your auth system
    const session = await getSession();
    return session.accessToken;
  },
});

Handling Token Expiry

Use onUnauthorized to handle expired tokens:

Handle Unauthorizedtypescript
const _sdk = new SpaceInvoices({
  accessToken: "YOUR_API_KEY",
  onUnauthorized: async (_response) => {
    // Token expired - refresh and retry
    await refreshToken();
    window.location.reload();
  },
});

Custom Base URL

For self-hosted or regional deployments:

Custom Base URLtypescript
const _sdk = new SpaceInvoices({
  accessToken: "YOUR_API_KEY",
  basePath: "https://eu.spaceinvoices.com",
});

Tree-Shakeable Configuration

When using tree-shakeable imports, call initSDK once at startup:

Tree-Shakeable Inittypescript
import { initSDK, invoices } from "@spaceinvoices/js-sdk";

// Initialize once at app startup
initSDK({
  accessToken: "YOUR_API_KEY",
  basePath: "https://eu.spaceinvoices.com",
});

// Now use individual modules anywhere
const _result = await invoices.list({ entity_id: "ent_123" });

Next Steps