Prerequisites
- TerminusDB running locally (install guide)
- A database with existing documents
What you'll achieve By the end of this guide, you will have retrieved documents from your TerminusDB database using the JavaScript client, the Python client, or the HTTP API.
Example database
Connect to TerminusDB first.
Get a single document
Retrieve a document by its ID:
const getDoc = async () => {
const doc = await client.getDocument({ id: "Player/Doug" })
console.log("Player/Doug:", doc)
}Returns:
{
"@id" : "Player/Doug",
"@type" : "Player",
"name" : "Doug",
"position": "Full Back"
}document = client.get_document("Player/Doug")Returns:
{
"@id" : "Player/Doug",
"@type" : "Player",
"name" : "Doug",
"position": "Full Back"
}curl -u admin:root \
"http://localhost:6363/api/document/admin/tdb-example-mydb/local/branch/main?id=Player/Doug"Returns:
{
"@id" : "Player/Doug",
"@type" : "Player",
"name" : "Doug",
"position": "Full Back"
}Get all documents
Retrieve every document in the database:
const getDocs = async () => {
const documents = await client.getDocument({ as_list: "true" })
console.log("All documents:", documents)
}Returns:
[
{
"@id" : "Player/Doug",
"@type" : "Player",
"name" : "Doug",
"position": "Full Back"
},
{
"@id" : "Player/George",
"@type" : "Player",
"name" : "George",
"position": "Center Back"
},
{
"@id" : "Player/Karen",
"@type" : "Player",
"name" : "Karen",
"position": "Center Forward"
}
]documents = list(client.get_all_documents())get_all_documents returns a generator — wrap it with list() if you need to iterate more than once.
[
{
"@id" : "Player/Doug",
"@type" : "Player",
"name" : "Doug",
"position": "Full Back"
},
{
"@id" : "Player/George",
"@type" : "Player",
"name" : "George",
"position": "Center Back"
},
{
"@id" : "Player/Karen",
"@type" : "Player",
"name" : "Karen",
"position": "Center Forward"
}
]curl -u admin:root \
"http://localhost:6363/api/document/admin/tdb-example-mydb/local/branch/main?as_list=true"Returns:
[
{
"@id" : "Player/Doug",
"@type" : "Player",
"name" : "Doug",
"position": "Full Back"
},
{
"@id" : "Player/George",
"@type" : "Player",
"name" : "George",
"position": "Center Back"
},
{
"@id" : "Player/Karen",
"@type" : "Player",
"name" : "Karen",
"position": "Center Forward"
}
]Counting documents
Available as of the 12.1 release candidates. Pass counted=true on the document listing endpoint to get the number of documents the listing would yield, without streaming them. The count includes subclasses when restricted by type, and follows the listing rules for subdocuments.
curl -u admin:root \
"http://localhost:6363/api/document/admin/tdb-example-mydb/local/branch/main?type=Player&counted=true"{ "@type": "api:DocumentCountResponse", "api:count": 2 }counted cannot be combined with id, ids or query; the server answers api:CountedNotSupportedForSelector.
Next steps
You've retrieved documents from TerminusDB. Next, you might want to:
- Edit documents — update the documents you just fetched
- Query with WOQL — use pattern-matching queries for complex filtering
- Delete documents — remove documents you no longer need