Add a Schema

Open inAnthropic

Prerequisites

What you'll achieve

By the end of this guide, you will have defined a schema (document types) in your TerminusDB database using the HTTP API, TypeScript, or Python.

Define a schema

A schema in TerminusDB defines your document types — their fields, data types, keys, and relationships. Schema documents are JSON objects with @type: "Class".

HTTP API

curl -u admin:root -X POST \
  "http://localhost:6363/api/document/admin/mydb?graph_type=schema&author=admin&message=Add+schema" \
  -H "Content-Type: application/json" \
  -d '[
    {"@type": "Class", "@id": "Country", "@key": {"@type": "Lexical", "@fields": ["name"]}, "name": "xsd:string"},
    {"@type": "Class", "@id": "Person", "@key": {"@type": "Lexical", "@fields": ["name"]}, "name": "xsd:string", "nationality": "Country"}
  ]'

Expected response:

Example: JSON
["terminusdb:///schema#Country", "terminusdb:///schema#Person"]
Example: TypeScript
const schema = [
  {
    "@type": "Class",
    "@id": "Country",
    "@key": { "@type": "Lexical", "@fields": ["name"] },
    "name": "xsd:string",
  },
  {
    "@type": "Class",
    "@id": "Person",
    "@key": { "@type": "Lexical", "@fields": ["name"] },
    "name": "xsd:string",
    "nationality": "Country",
  },
];

const result = await client.addDocument(schema, { graph_type: "schema" });
console.log("Schema created:", result);
// ["terminusdb:///schema#Country", "terminusdb:///schema#Person"]

Schema concepts

ConceptDescriptionExample
@type: "Class"Declares a document type{"@type": "Class", "@id": "Person"}
@keyDetermines how document IDs are generated{"@type": "Lexical", "@fields": ["name"]}
Field typesProperties use XSD types or references to other classes"name": "xsd:string", "nationality": "Country"
@subdocumentEmbedded objects without independent identity"@subdocument": []

Key strategies

  • Lexical — deterministic IDs from field values (e.g., Person/Alice)
  • Hash — content-addressed IDs from a hash of specified fields
  • ValueHash — IDs derived from all field values (immutable documents)
  • Random — auto-generated unique IDs (default)

Next steps

Was this helpful?