SDK
Install @spacefast/sdk and make typed calls against the Spacefast REST API.
The Spacefast SDK is a typed TypeScript client generated from the same OpenAPI document as the REST API reference. Use it when you want request and response types without writing HTTP by hand. The API remains the contract. Authentication, envelopes, idempotency, and error codes behave exactly as in the REST API guide.
Install
npm install @spacefast/sdkpnpm add @spacefast/sdkyarn add @spacefast/sdkbun add @spacefast/sdkPin the version you install. The SDK requires Node.js 20 or newer.
Create a client
import { function createSpacefastClient(config: SpacefastClientConfig): {
get: <P extends PathsWithMethod<"get">>(path: P, ...args: RequestArgs<Operation<P, "get">>) => Promise<UnwrapEnvelope<OrUnknown<SuccessBody<Operation<P, "get">>>>>;
post: <P extends PathsWithMethod<"post">>(path: P, ...args: RequestArgs<Operation<P, "post">>) => Promise<UnwrapEnvelope<OrUnknown<...>>>;
patch: <P extends PathsWithMethod<...>>(path: P, ...args: RequestArgs<...>) => Promise<UnwrapEnvelope<OrUnknown<...>>>;
put: <P extends PathsWithMethod<...>>(path: P, ...args: RequestArgs<...>) => Promise<UnwrapEnvelope<OrUnknown<...>>>;
delete: <P extends PathsWithMethod<...>>(path: P, ...args: RequestArgs<...>) => Promise<UnwrapEnvelope<OrUnknown<...>>>;
}
createSpacefastClient } from "@spacefast/sdk";
const const token: string | undefinedtoken = var process: NodeJS.Processprocess.NodeJS.Process.env: NodeJS.ProcessEnvThe `process.env` property returns an object containing the user environment.
See [`environ(7)`](http://man7.org/linux/man-pages/man7/environ.7.html).
An example of this object looks like:
```json
{
"TERM": "xterm-256color",
"SHELL": "/usr/local/bin/bash",
"USER": "maciej",
"PATH": "~/.bin/:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin",
"PWD": "/Users/maciej",
"EDITOR": "vim",
"SHLVL": "1",
"HOME": "/Users/maciej",
"LOGNAME": "maciej",
"_": "/usr/local/bin/node"
}
```
It is possible to modify this object, but such modifications will not be
reflected outside the Node.js process, or (unless explicitly requested)
to other `Worker` threads.
In other words, the following example would not work:
```bash
node -e 'process.env.foo = "bar"' && echo $foo
```
While the following will:
```js
import { env } from 'node:process';
env.foo = 'bar';
console.log(env.foo);
```
Assigning a property on `process.env` will implicitly convert the value
to a string. **This behavior is deprecated.** Future versions of Node.js may
throw an error when the value is not a string, number, or boolean.
```js
import { env } from 'node:process';
env.test = null;
console.log(env.test);
// => 'null'
env.test = undefined;
console.log(env.test);
// => 'undefined'
```
Use `delete` to delete a property from `process.env`.
```js
import { env } from 'node:process';
env.TEST = 1;
delete env.TEST;
console.log(env.TEST);
// => undefined
```
On Windows operating systems, environment variables are case-insensitive.
```js
import { env } from 'node:process';
env.TEST = 1;
console.log(env.test);
// => 1
```
Unless explicitly specified when creating a `Worker` instance,
each `Worker` thread has its own copy of `process.env`, based on its
parent thread's `process.env`, or whatever was specified as the `env` option
to the `Worker` constructor. Changes to `process.env` will not be visible
across `Worker` threads, and only the main thread can make changes that
are visible to the operating system or to native add-ons. On Windows, a copy of `process.env` on a `Worker` instance operates in a case-sensitive manner
unlike the main thread.env.string | undefinedSPACEFAST_TOKEN;
if (!const token: string | undefinedtoken) throw new var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error("Set SPACEFAST_TOKEN");
const const client: {
get: <P extends PathsWithMethod<"get">>(path: P, ...args: RequestArgs<Operation<P, "get">>) => Promise<UnwrapEnvelope<OrUnknown<SuccessBody<Operation<P, "get">>>>>;
post: <P extends PathsWithMethod<...>>(path: P, ...args: RequestArgs<...>) => Promise<UnwrapEnvelope<OrUnknown<...>>>;
patch: <P extends PathsWithMethod<...>>(path: P, ...args: RequestArgs<...>) => Promise<UnwrapEnvelope<OrUnknown<...>>>;
put: <P extends PathsWithMethod<...>>(path: P, ...args: RequestArgs<...>) => Promise<UnwrapEnvelope<OrUnknown<...>>>;
delete: <P extends PathsWithMethod<...>>(path: P, ...args: RequestArgs<...>) => Promise<UnwrapEnvelope<OrUnknown<...>>>;
}
client = function createSpacefastClient(config: SpacefastClientConfig): {
get: <P extends PathsWithMethod<"get">>(path: P, ...args: RequestArgs<Operation<P, "get">>) => Promise<UnwrapEnvelope<OrUnknown<SuccessBody<Operation<P, "get">>>>>;
post: <P extends PathsWithMethod<"post">>(path: P, ...args: RequestArgs<Operation<P, "post">>) => Promise<UnwrapEnvelope<OrUnknown<...>>>;
patch: <P extends PathsWithMethod<...>>(path: P, ...args: RequestArgs<...>) => Promise<UnwrapEnvelope<OrUnknown<...>>>;
put: <P extends PathsWithMethod<...>>(path: P, ...args: RequestArgs<...>) => Promise<UnwrapEnvelope<OrUnknown<...>>>;
delete: <P extends PathsWithMethod<...>>(path: P, ...args: RequestArgs<...>) => Promise<UnwrapEnvelope<OrUnknown<...>>>;
}
createSpacefastClient({
baseUrl: stringAPI origin, e.g. `https://api.spacefast.com`.baseUrl: "https://api.spacefast.com",
apiKey?: string | undefinedManaged account token; sent as `Authorization: Bearer ...`.apiKey: const token: stringtoken,
});
The client sends apiKey as Authorization: Bearer …. For browser
sessions that rely on cookies, pass credentials: "include" instead of an
API key. Mint keys with sf api-keys or the
dashboard.
Make typed calls
const me = await client.get("/v1/me");
console.log(me);
const spaces = await client.get("/v1/spaces", {
query: { limit: 20 },
});
Mutations take a JSON body and an optional idempotencyKey:
const space = await client.post("/v1/spaces", {
body: { slug: "docs", title: "Docs" },
idempotencyKey: "01J-create-docs",
});
Path parameters use a path object:
const detail = await client.get("/v1/spaces/{spaceId}", {
path: { spaceId: "spc_123" },
});
The client unwraps successful JSON envelopes to the data payload.
Failures throw SpacefastApiError carrying the problem document:
status, code, type (the docs URL), detail as the message,
pointer on validation errors, requestId, and retry.
Exports
| Import | Purpose |
|---|---|
@spacefast/sdk |
createSpacefastClient, SpacefastApiError |
@spacefast/sdk/schema |
Generated OpenAPI path types |
@spacefast/sdk/transport |
Envelope helpers and transport types |
@spacefast/sdk/openapi.json |
The generated OpenAPI document |
If a capability is missing from the generated release that you installed,
call the documented REST endpoint with sf api or fetch rather than
guessing a method name.