Protocol v3 · Sections 2 to 5
Records and schemas
Canonical JSON
Each hashed JSON document MUST be serialized as RFC 8785 (JCS): no insignificant whitespace; strings escaped as by JSON.stringify; numbers serialized as by ECMAScript Number.prototype.toString, with -0 as 0; object members ordered by the UTF-16 code units of their keys.
Note: ECMAScript objects enumerate integer-like keys ("9", "10") first regardless of insertion order. Sorting keys into a new object and calling JSON.stringify does not produce JCS; an implementation MUST emit object members as strings itself.
Records and lines
A record is an id (string), a type, and data (any JSON value). Its key is (type, id), and within a version a key names at most one record. A sorted-record-file holds two kinds of line, each UTF-8 and ending with \n:
record line: '{"id":' + JCS(id) + ',"type":' + JCS(type) + ',"data":' + JCS(data) + '}'
removal line: '{"id":' + JCS(id) + ',"removed":true,"type":' + JCS(type) + '}'
size of a line = its length in bytes, without the "\n"
record hash = SHA-256(record line) 64 lowercase hex characters
a removal line has no hash- A record line is the record’s canonical form: a fixed envelope,
id,type,datain that order, with onlydatacanonicalized. The record hash is the SHA-256 of the line. - A removal line says the record with that key is removed.
- An upload’s input lines are a record,
{"id", "type", "data"}, or a removal,{"id", "type", "removed": true}, with exactly those members. A line with any other member is refused.
Lines of one file, in key order:
{"id":"collection","type":"$meta","data":{"readme":"Weekly snapshot"}}
{"id":"Publication","type":"$schema","data":{"properties":{"title":{"type":"string"}},"type":"object"}}
{"id":"pub-001","type":"Publication","data":{"title":"Notes"}}
{"id":"pub-002","removed":true,"type":"Publication"}Strings are not Unicode-normalized: é as U+00E9 and as U+0065 U+0301 are distinct ids.
Reserved types
Types that begin with $ are reserved. A collection’s schemas, assets, and metadata are rows of these types, in the same files as its records.
| Type | Id | Data |
|---|---|---|
$schema | a type name | that type’s JSON Schema |
$asset | an asset’s SHA-256, hex | {"size": n, "mediaType": string or null} |
$meta | collection | the collection’s metadata object |
A publisher’s own types MUST NOT begin with $, and no other $ type is valid. A publisher’s type also MUST NOT begin with a character that sorts before $ (U+0020 to U+0023), so that the reserved rows sort before all records, and MUST NOT be __proto__. A type or an id with a lone surrogate is not valid.
Key order
Keys are ordered by type, then by id. Each is compared lexicographically by its UTF-8 encoding, octet by octet, a proper prefix first: Unicode code point order. Implementations MUST NOT compare UTF-16 code units (ECMAScript’s default < and sort()), which differ when one string has a character at or above U+10000 where the other has one in U+E000–U+FFFF.
Input rules
A writer MUST apply these rules to each line it uploads, and a server to each line it receives. They apply to the whole line and are evaluated on the source text, since a parsed value no longer carries the information they require. Schemas are subject to the duplicate key, unsafe integer, and lone surrogate rules.
| Rule | Rejected if | Code |
|---|---|---|
| Syntax | Not JSON as defined by RFC 8259, including an unterminated string or invalid escape | syntax |
| Duplicate keys | An object with two members whose keys are equal after unescaping | duplicate_key |
| Unsafe integers | An integer literal (no fraction, no exponent) of magnitude above 2⁵³ − 1. Literals with a fraction or exponent are accepted as IEEE 754 binary64 | unsafe_integer |
| Lone surrogates | A UTF-16 surrogate code unit, literal or \u-escaped, not part of a pair, in a string or key | lone_surrogate |
| Depth | The line nesting more than 65 levels: each object or array is a level and the envelope is level 1, so data may nest 64 | too_deep |
| Envelope | Not an object; a record whose members are not exactly id, type, and data; or a removal whose members are not exactly id, type, and "removed": true | bad_envelope |
| Record id | Absent, not a string, empty, or over 1,024 UTF-8 bytes | bad_id |
| Type | Absent, not a string, empty, over 128 UTF-8 bytes, beginning with ".", or containing /, \, U+0000–U+001F, or U+007F; beginning with "$" other than a reserved type; beginning with a character that sorts before "$" (U+0020–U+0023); or "__proto__" | bad_type |
| Record size | A record line longer than 8,388,608 bytes | record_too_large |
A line that breaks several rules is reported with the code of the first in this order: the first violation in text order of an unterminated string or invalid escape (syntax), duplicate_key, unsafe_integer, lone_surrogate, or too_deep, whether or not the text is otherwise JSON; syntax for any other text that is not JSON; bad_envelope for a non-object; bad_id or bad_type; bad_envelope for the wrong members; record_too_large. The codes are part of the protocol.
The push API on underlay.org takes record lines with a looser envelope (see Push and pull), and writes them into files as the lines above.
Schemas
A type’s schema is a JSON Schema draft-07 object, held as the data of the type’s $schema row. Schema hash = SHA-256 of JCS(schema). A schema MUST be rejected if:
- its canonical form exceeds 262,144 bytes;
- any
patternorpatternPropertieskey exceeds 256 UTF-16 code units. Apatterninsideconst,enum,default, orexamplesis data and is not limited; - the type it is given for is not a valid type.
A collection is wholly public or private: nothing in a schema makes a type or a field private. underlay.org refuses a schema in a public collection whose root has "private": true, and any schema with "private": true on a property, since an author who writes either means to hide data that would be published.
Within a version:
- A record of a publisher’s type
TMUST satisfy the$schemarow forT, and a type with records MUST have a$schemarow. - When a commit changes a schema, each record of that type the version carries over MUST satisfy the new schema, and the commit MUST be refused if any does not. A change that only relaxes the schema (any document valid before is valid after) needs no check of the records carried over.
- A record whose line a server withholds is not checked.
Validation
A schema MUST be rejected unless:
- its root
$schema, if present, ishttp://json-schema.org/draft-07/schema, with or without a trailing#; - it is valid against the draft-07 meta-schema;
- each pattern compiles as an ECMAScript regular expression with the
uflag; - each
$refresolves within the schema or to the draft-07 meta-schema, against the base URIhttps://schema.underlay.invalid/unless a$idsets another.
Records are validated under draft-07 with these refinements:
- keywords adjacent to
$refapply, as in draft 2019-09; - keywords draft-07 does not define are ignored (
unevaluatedProperties,prefixItems, draft-04id, …);$defsand$anchorare honored; multipleOfm accepts x when r = x mod m satisfies |r| < 1.1920929 × 10⁻⁷ or |m − r| < 1.1920929 × 10⁻⁷;- string length is measured in Unicode code points;
formatconstrains strings only, for the formats ajv-formats 3.0 defines in full mode (date-timeandtimerequire a time zone); other format names are ignored.
Only the verdict is normative; error messages are not.
Assets
An asset is a byte string named by its SHA-256, held by a collection beside its records: a PDF, an image, a dataset’s attachments. A collection holds an asset when a $asset row names it; the row’s size MUST be the length of the bytes. A $asset row that adds an asset to a collection is accepted only if the asset’s bytes were sent to that collection, or one of its versions already names it. That the server stores the bytes for another collection is never enough.
Records attach assets with asset references (see Asset references). Informative. On underlay.org a push adds a $asset row for each asset its records attach, or it declares, that the version before did not name.
Underlay ids
Outside the protocol’s own objects (in record data, URLs, API responses, and citations) a record, an asset, or a version is named by an Underlay id:
ul:rec:<hex> a record: the hash of its line
ul:asset:<hex> an asset: the SHA-256 of its bytes
ul:ver:<hex> a version: the SHA-256 of its commit
<hex> = 64 lowercase hex charactersAn id is exact: lowercase, with no other spelling. A schema is a $schema record, so ul:rec: names schemas too; other kinds are reserved. Inside a commit, layout, file entry, or receipt a hash is bare hex, since the member it is in says what it is, so no way of writing an id is part of anything hashed. There is no version number in an id: every kind is the SHA-256 of stored bytes, and those bytes say their own format.
Informative. In URL paths and version selectors, underlay.org takes a ul: id or bare hex. /api/links/resolve needs a ul: id, since bare hex doesn’t say what kind of thing it names.
Links
A record points at another record, an asset, or a version with a link: an object, at any depth of its data, with a $link member whose value is an Underlay id. The prefix says what kind of thing the link points at:
| Target | $link | Hash | Members that say where it is |
|---|---|---|---|
| A record | ul:rec:<hex> | the record’s hash (the hash of its line) | collection, type, id |
| An asset | ul:asset:<hex> | the SHA-256 of its bytes | collection |
| A version | ul:ver:<hex> | the SHA-256 of its commit (the version id) | collection |
One of each, in a record’s data:
{
"title": "Notes on a dataset",
"derivedFrom": {
"$link": "ul:rec:9f2c…",
"collection": "4f7d1c2e-…",
"type": "work",
"id": "10.1234/abc"
},
"figure": {
"$link": "ul:asset:7b1e…",
"collection": "4f7d1c2e-…"
},
"usedVersion": {
"$link": "ul:ver:41d0…",
"collection": "4f7d1c2e-…"
}
}Members
| Member | Value | |
|---|---|---|
$link | Required | An Underlay id, as above |
collection | Optional | The id of a collection that holds the target, never its handle: letters, digits, ., _, or -, up to 128 characters |
type, id | Optional | For a record, its type and id in that collection: strings of 1 to 1,024 characters |
| Any other | Optional | The publisher’s, such as a label or a relation |
- The hash is what the link points at.
collection,type, andidonly say where the target was found, so a reader can open it. - Name the field that holds a link for what it means (
derivedFrom,cites): readers tell links apart by where they are. - A link object is not searched for further links.
What a server refuses
A server MUST refuse a record with an object whose $link is not an Underlay id, whose collection is not a collection id, or whose type or id is not a string of 1 to 1,024 characters. A pasted hash or an old prefix is refused, not stored as data that looks like a link:
$link given | Write instead |
|---|---|
9f2c… | ul:rec:9f2c…, ul:asset:9f2c…, or ul:ver:9f2c…: bare hex doesn’t say its kind |
sha256:9f2c… | ul:asset:9f2c… |
ulv3:9f2c… | ul:ver:9f2c… |
UL:REC:9F2C… | ul:rec:9f2c…: ids are lowercase |
42, {}, null | a string |
A server MUST NOT refuse a link because of its target, which may be private, held elsewhere, taken down, or never published. A link is a claim made by the record that holds it.
Links and asset references
A link to an asset only points at it, wherever it is; it does not make the record’s collection hold it. Attaching an asset is what an asset reference does (see Asset references). An object with both $link and $asset is refused.
Informative. What a server builds from links (what points at a record, where data came from) is not part of the protocol. Since links name targets by hash, following them always ends. On underlay.org, GET /api/links/resolve?to=<id>&collection=<id> (with type and id for a record) redirects to where the target can be read, and a record’s page has Copy as $link.
Asset references
A record attaches an asset with an asset reference: an object, at any depth of its data, with a $asset member whose value is the asset’s Underlay id.
{
"title": "Notes on a dataset",
"cover": {
"$asset": "ul:asset:7b1e…",
"mediaType": "image/png",
"fileName": "cover.png"
}
}Members
| Member | Value | |
|---|---|---|
$asset | Required | The asset’s Underlay id: ul:asset:<hex> |
mediaType | Recommended | What the bytes are: an IANA media type, type/subtype (RFC 6838), such as image/png, with optional parameters in printable ASCII |
fileName | Recommended | What to call the bytes when they’re saved: a file name, with no /, \, control characters, or bidirectional formatting characters, not . or .., at most 255 characters |
| Any other | Optional | The publisher’s, such as where the bytes came from |
- An asset reference is read as a link to its asset. It is not searched for links or further references.
- The collection must hold what its records attach. A server that builds a commit from records adds a
$assetrow for each attached asset the collection doesn’t hold yet, and refuses the change if that asset’s bytes were never sent to the collection (see Assets). A client that builds its own commits SHOULD do the same. - A collection’s
$assetrow takes itsmediaTypefrom the records that attach the asset: a server that builds a commit sets it to the one a reference in the commit’s records declares (the first, when they differ), over the type the bytes were uploaded with, for an asset the collection holds already as well as a new one. A client that builds its own commits SHOULD do the same. The type is the collection’s: another collection holding the same bytes declares its own. - A
mediaTypeorfileNamethat isn’t well formed declares nothing; the record is not refused for it. - A reader saving an asset SHOULD name it by the reference’s
fileName. A server serves an asset with its collection’smediaType, except that it MAY serve a type a browser would render or run (HTML, SVG, XML) asapplication/octet-stream.
What a server refuses
A server MUST refuse a record with an object whose $asset is not a ul:asset: id, or that has both $link and $asset. A pasted hash is refused, not stored as data that looks like a reference:
$asset given | Write instead |
|---|---|
9f2c… | ul:asset:9f2c… |
sha256:9f2c… | ul:asset:9f2c… |
UL:ASSET:9F2C… | ul:asset:9f2c…: ids are lowercase |
ul:rec:9f2c…, ul:ver:9f2c… | a ul:asset: id: $asset attaches only assets; use $link to point at a record or a version |
42, {}, null | a string |
$link and $asset together | one of them: $link points at an asset, $asset attaches it |
Informative. On underlay.org the push refuses such a record with 422 and a message that says where it is, such as $asset at /cover is a sha256: hash: write ul:asset:<hex>.
$file references
A $file member is not an asset reference, and a server refuses a record that holds one, with a message such as $file at /pdf is not read: attach an asset with {"$asset": "ul:asset:<hex>"}. Records written before November 2026 used $file; they were rewritten.