Reading and writing space records works similarly to public records. The reference lex TypeScript SDK provides another package called lex-space for working with space data:
npm install @atproto/lex @atproto/lex-space
Records in a space are ordinary atproto records, defined by Lexicons, read and written with the same helpers as public data. The differences are in where they live and who can read them.
Creating a space
Spaces have to be created before being written to. Writing to a space that doesn't exist fails with SpaceNotFound.
The identity who creates the space becomes the space authority. To create a space, call createSpace() with an authenticated client and a Lexicon for that Space:
import { SpaceClient, personalspace } from '@atproto/lex-space'
import { com } from './lexicons/index.js'
const space = await SpaceClient.createSpace(client.agent, com.example.bookmarks.main, {
policy: personalspace.policy.$build({}),
})
You can also assign an access policy to a space at creation time. There are three defaults:
personalspace: only the space authority has access.memberspace: the authority and any other invited users have access.managedspace: access is delegated to an application which handles permissions.
Opening a space
Once a Space has been created, call openSpace() on it:
import { SpaceClient } from '@atproto/lex-space'
// by authority (the key can be left out for space types with a literal key)
const space = SpaceClient.openSpace(
com.example.bookmarks.main,
{ authority: client.assertDid },
{ agent: client.agent },
)
// or by URI
const forum = SpaceClient.openSpace(
com.example.forum.main,
'at:space/did:plc:abc/com.example.forum/general',
{ agent: client.agent },
)
This performs credential management as needed in the background. A space credential is fetched at runtime, then cached and renewed.
Writing records
create, put, and delete operations on space records work much like they do on public records. create and put take a record built with $build(), and delete takes the collection's NSID ($type). Writes always go to the user's own repository, in the space.
import { currentDatetimeString } from '@atproto/lex'
const { uri, cid } = await space.create(
com.example.bookmark.$build({
url: 'https://atproto.com',
createdAt: currentDatetimeString(),
}),
)
await space.put(com.example.bookmark.$build({ url, createdAt, note: 'read later' }), { rkey })
await space.delete(com.example.bookmark.$type, { rkey })
Blobs are uploaded as normal, then referenced from a space record.
Reading records
As with public records, you can list records matching a given Lexicon in a space, by its NSID:
const { records, cursor } = await space.list(com.example.bookmark.$type, { limit: 50 })
for (const { rkey, valid, value } of records) {
if (valid) console.log(rkey, value.url)
}
Or get records individually:
const bookmark = await space.get(com.example.bookmark.$type, { rkey })
Read operations target a user's own repository by default. You can also read other user repositories from the Spaces you're in:
const posts = await forum.list(com.example.forum.post.$type, { repo: 'did:plc:xyz' })
You also need access to fetch blobs with space.getBlob():
import { getBlobCid } from '@atproto/lex'
const post = await forum.get(com.example.forum.post.$type, { repo: 'did:plc:xyz', rkey })
const { body } = await forum.getBlob({
repo: 'did:plc:xyz',
cid: getBlobCid(post.value.image),
})
// body: Uint8Array
Space types
A space type is a Lexicon with type: "space", listing the record collections the space is meant to hold. A required key property controls the space key; literal:self means one space of this type per account, like a profile record:
{
"lexicon": 1,
"id": "com.example.bookmarks",
"defs": {
"main": {
"type": "space",
"key": "literal:self",
"title": "Bookmarks",
"collections": ["com.example.bookmark"]
}
}
}
You can install and build space types like any other Lexicon (Developing with Lexicons).
You can also list a user's spaces by spaceType:
import { listSpaces } from '@atproto/lex-space'
const { spaces } = await client.call(listSpaces, {
spaceType: com.example.forum.$spaceType,
})
App Permissions
Apps are granted access to spaces with space: OAuth scopes, by space type and authority. See Permission Requests:
space:com.example.bookmarks?manage=create
space:com.example.forum?authority=*
space:com.example.forum?authority=*&action=read
Creating a space needs manage=create.
authority=self refers to a user's own spaces. authority=* covers spaces where the user is a member.
read reads every repo in a space; read_self reads only the user's own.
For every space: parameter, and how to include space permissions in a permission set, see Space permissions.
Addressing Space Records
Space record URIs are constructed slightly differently from public records. They begin with at:space/ and include the space authority and space type:
at:space/{authority}/{spaceType}/{skey}/{author}/{collection}/{rkey}
at:space/did:plc:abc/com.example.bookmarks/self/did:plc:abc/com.example.bookmark/3m...