Install and set up

Add the package to your app, create API keys, and choose the project your calls work in.

Install the package

The package is digitospace on npm. It runs on Node 20.3 or newer, Bun, Deno, and Cloudflare Workers.

npm install digitospace

Choose a project

Every call works in one project. Open the project in the console and copy its ID from Settings. It is 26 lowercase letters and digits.

Create an API key

A key belongs to your organisation, not to a person, so it keeps working when people join or leave your team. Create one in the console under API keys, and choose the permissions it needs in each service you use, such as Storage files: Upload for space.fs.write and Call models for space.ai.chat, and the projects it may work in. One key can cover every product you use. See API keys.

To keep products apart, you can create a key for each and pass them as apiKeys. See createSpace().

Set the environment variables

space reads two variables when it makes its first call:

VariableHolds
DIGITOSPACE_API_KEYThe API key, starting with dsk_
DIGITOSPACE_PROJECTThe project ID
export DIGITOSPACE_API_KEY="dsk_…"
export DIGITOSPACE_PROJECT="your-project-id"

Then import space and call it:

check.ts
import { space } from "digitospace"

for await (const bucket of space.fs.buckets.list()) {
  console.log(bucket.name)
}

A list of your buckets, or nothing at all when the project has none yet, means everything works.

Use a key for each product

When you use more than one product, give each its own key with apiKeys in createSpace(). A product with no key of its own uses apiKey, or DIGITOSPACE_API_KEY.

space.ts
import { createSpace } from "digitospace"

export const space = createSpace({
  apiKeys: {
    fs: process.env.STORAGE_API_KEY,
    ai: process.env.AI_API_KEY,
    schedule: process.env.SCHEDULER_API_KEY,
    hooks: process.env.WEBHOOKS_API_KEY,
  },
  project: process.env.DIGITOSPACE_PROJECT,
})

Import this space from your own module wherever you would import it from digitospace. Each part only needs its key when you call it, so leave out the products you do not use.

Cloudflare Workers

A worker gets its secrets in env, not in the process environment, so pass them to createSpace():

worker.ts
import { createSpace } from "digitospace"

interface Env {
  DIGITOSPACE_API_KEY: string
  DIGITOSPACE_PROJECT: string
}

export default {
  async fetch(request: Request, env: Env): Promise<Response> {
    const space = createSpace({
      apiKey: env.DIGITOSPACE_API_KEY,
      project: env.DIGITOSPACE_PROJECT,
    })
    const text = await space.fs.read("notes/today.txt", { as: "text" })
    return new Response(text)
  },
}

When something is missing

What happenedCode
No key for the product you calledmissing_api_key
No projectmissing_project
The key or project is not in the right formvalidation_failed
The key is unknown, deleted, or expiredunauthenticated
The key lacks the permission, or is not allowed in the projectforbidden

See SpaceError for every code.

Last updated