Build a Private App

Store private data in a personal space

In this tutorial, you'll use the Spaces primitives to build a private calendar on atproto. This will be data that only you — and any authorized apps — can read. You'll use Lexicon Community-defined calendar Lexicons, along with the Spaces TypeScript SDK.

Prerequisites

You should have installed:

  • Node.js 22+

On platforms supported by homebrew, you can install it with:

brew install node

You'll also need an account on a PDS that supports Spaces. As of December 1, 2026, the reference PDS includes support for Spaces.

Part 1: Project Setup

Create a new project directory and install the SDK and other project dependencies:

mkdir my-calendar
cd my-calendar
npm init -y --init-type=module
npm i -D typescript @types/node dotenv @atproto/lex @atproto/lex-password-session @atproto/lex-space
npx tsc --init --verbatimModuleSyntax false

You'll also need the lex CLI tool and tsx installed globally, and the community.lexicon.calendar Lexicons installed in this project to read and write calendar data:

npm install -g @atproto/lex tsx
lex install community.lexicon.calendar.collection community.lexicon.calendar.event
lex build

Part 2: Log in

Create a new file at src/calendar.ts. This file will contain some shared methods for your app.

Start by importing the packages you'll need, and setting up the authenticated client:

import { Client } from '@atproto/lex'
import { PasswordSession } from '@atproto/lex-password-session'
import { SpaceClient, listSpaces, personalspace } from '@atproto/lex-space'
import * as dotenv from 'dotenv'
import * as community from './lexicons/community.js'

dotenv.config()

export const calendarType = community.lexicon.calendar.collection.main
export const event = community.lexicon.calendar.event

export async function login() {
  const session = await PasswordSession.login({
    service: process.env.PDS_URL!,
    identifier: process.env.HANDLE!,
    password: process.env.PASSWORD!,
  })
  return new Client(session)
}

Part 3: Open your calendar

Next, add an openCalendar() function:

  • This space will be keyed as personal because you aren't expecting to share it.
  • Look for it with listSpaces; create it the first time.
  • Call createSpace() with a personalspace policy: only the authority has access.
  • Everything here goes to your own repo on your own PDS, so no further credentials are needed.
  • Finally, return the Space with openSpace().
export async function openCalendar(client: Client) {
  const { spaces } = await client.call(listSpaces, {
    spaceType: calendarType.$spaceType,
    spaceDid: client.did,
  })

  if (!spaces.some((s) => s.space.endsWith('/personal'))) {
    return SpaceClient.createSpace(client.agent, calendarType, {
      skey: 'personal',
      policy: personalspace.policy.$build({}),
    })
  }

  return SpaceClient.openSpace(
    calendarType,
    { authority: client.assertDid, skey: 'personal' },
    { agent: client.agent },
  )
}

Part 4: Add and remove events

Next, create another file at src/index.ts. Here, you'll use the helper methods from src/calendar.ts to actually read and write records in your space, to interact with your calendar.

For this demo app, you'll add some CLI methods to create, delete, and list records. These work similarly to writing public data, except that you build the record with event.$build(), and pass the collection's NSID (event.$type) to delete and list. The community.lexicon.calendar.event Lexicon needs name, startsAt, and createdAt, so you'll handle those as command line input:

import { currentDatetimeString, toDatetimeString } from '@atproto/lex'
import { login, openCalendar, event } from './calendar.js'

async function main() {
  const client = await login()
  const calendar = await openCalendar(client)
  const [command, ...args] = process.argv.slice(2)

  switch (command) {
    case 'add': {
      const [startsAt, ...name] = args
      await calendar.create(
        event.$build({
          name: name.join(' '),
          startsAt: toDatetimeString(startsAt),
          createdAt: currentDatetimeString(),
        }),
      )
      break
    }
    case 'rm':
      await calendar.delete(event.$type, { rkey: args[0] })
      break
  }

  const { records } = await calendar.list(event.$type)
  for (const { rkey, valid, value } of records) {
    if (valid) console.log(`${value.startsAt}  ${value.name}  (${rkey})`)
  }
}

main()

Now you're ready to test the script. Start by setting environment variables in your shell so you can authenticate to your PDS:

PDS_URL= # a Spaces-enabled PDS, eg 'https://pds.example.com'
HANDLE= # eg 'alice.example.com'
PASSWORD= # your account password, not an app password

Then, run the script as npx tsx src/index.ts with some sample input:

npx tsx src/index.ts add 2026-12-01T18:00 dentist
npx tsx src/index.ts add 2026-12-05T10:00 farmers market
npx tsx src/index.ts

The last command, run on its own without add, should read the events you've written:

2026-12-01T18:00:00.000Z  dentist  (3m...)
2026-12-05T10:00:00.000Z  farmers market  (3m...)

Part 5: View it in your browser

Finally, you can make a minimal interface to display this data in a browser.

Create src/server.ts, which will use a node:http server to read your calendar and render it as HTML on localhost.

import { createServer } from 'node:http'
import { login, openCalendar, event } from './calendar.js'

const client = await login()
const calendar = await openCalendar(client)

createServer(async (req, res) => {
  const { records } = await calendar.list(event.$type)
  const items = records
    .filter((r) => r.valid)
    .map((r) => r.value)
    .sort((a, b) => (a.startsAt ?? '').localeCompare(b.startsAt ?? ''))
    .map((e) => `<li><time>${new Date(e.startsAt!).toLocaleString()}</time> ${escape(e.name)}</li>`)
    .join('')

  res.setHeader('Content-Type', 'text/html')
  res.end(`<!doctype html><title>My calendar</title><h1>My calendar</h1><ul>${items}</ul>`)
}).listen(3000, '127.0.0.1')

console.log('http://127.0.0.1:3000')

function escape(s: string) {
  return s.replace(/[&<>"]/g, (c) => `&#${c.charCodeAt(0)};`)
}

Run it with npx tsx:

npx tsx src/server.ts

Then open http://127.0.0.1:3000 in your browser:

You've now written and read Spaces data, and viewed it in a browser!

If you want to verify that this data is a) private, and b) can be read by other apps supporting Spaces, you can use an atproto browser like pdsls, following the walkthrough on our blog under Using Spaces across Apps. Log in with Spaces access to see the space and its records.

Conclusion

This is one of the most minimal examples of working with atproto Spaces. You wrote logic to create a new Space, wrote to the space, and read it back on the CLI and in a web interface. atproto can be as straightforward as that: among other things, it's a content-addressible storage interface, now with support for non-public data!

For another example of using atproto Spaces for private data, see Airspace!

Next, refer to Spaces data for a full set of read-write operations.