---
title: Query instance metadata
url: https://docs.triply.cc/how-to/query-instance-metadata/
description: Query the accounts, datasets, saved queries, stories, assets and flows of a TriplyDB instance
  with SPARQL, from the command line or from a query on a dataset.
availability:
- 'Preview: This feature is switched on per instance, by arrangement. Get in touch if you would like access.'
---

# Query instance metadata

The instance SPARQL endpoint answers questions about a TriplyDB instance itself: which datasets an
account owns, which saved queries run against a dataset, which assets were uploaded last week,
which flow runs failed. One query there can combine what would otherwise take many
[HTTP API](https://api.triplydb.com/api-docs) requests. Every class and property it exposes is
listed in [Instance SPARQL endpoint](https://docs.triply.cc/reference/instance-sparql-endpoint/).

On this page, `API` stands for the instance's API address, such as `api.triplydb.com`.

## Send a query to the endpoint

Send the query to `https://API/sparql`. To see what you see when logged in, add an
[API token](https://docs.triply.cc/how-to/create-an-api-token/) in an `Authorization: Bearer` header:

```sh
curl -H "Authorization: Bearer TRIPLYDB_TOKEN" \
     --data-urlencode "query=select * { ?dataset a <https://triplydb.com/Triply/triplydb/def/Dataset> }" \
     https://API/sparql
```

Leave the header out and you get what an anonymous visitor sees: public resources only.

## Query it from a dataset

To combine instance metadata with the data in a dataset, wrap the metadata part of a query in a
`service` clause and [run it on the dataset](https://docs.triply.cc/how-to/query-data/#run-a-sparql-query). The endpoint
answers with your access, as if you had sent your own token:

```sparql
prefix datasetDef: <https://triplydb.com/Triply/triplydb/def/Dataset/>

select ?dataset ?name where {
  service <https://API/sparql> {
    ?dataset datasetDef:name ?name .
  }
}
limit 10
```

## Write a query

Find the property in the [reference](https://docs.triply.cc/reference/instance-sparql-endpoint/#classes-and-properties).
A property is named by its class and the key that holds the value in the API's JSON, so if you
know the API you can guess most of them: the `name` of a dataset is `datasetDef:name`, with
`datasetDef:` standing for `https://triplydb.com/Triply/triplydb/def/Dataset/`.

Follow an IRI to the resource it names. A dataset's owner is the IRI of the account, so
`datasetDef:owner/accountDef:accountName` reaches the owner's short name.

Escape the slash in a nested property. A key such as `uploadedBy/accountName` contains a `/`, which
SPARQL reads as a property path unless you escape it:
`assetVersionDef:uploadedBy\/accountName`.

Match list items with `rdf:rest*/rdf:first`. Tags, prefixes, asset versions and other lists are
`rdf:List`s, so `datasetDef:tags/rdf:rest*/rdf:first ?tag` binds `?tag` to each tag in turn.

## Examples

Each example answers one question about the instance with a complete query for the endpoint.

### List the datasets of an account

List all datasets owned by the `academy` group, with their licenses where one is filled in:

```sparql
prefix accountDef: <https://triplydb.com/Triply/triplydb/def/Account/>
prefix datasetDef: <https://triplydb.com/Triply/triplydb/def/Dataset/>

select ?dataset ?name ?license where {
  ?owner accountDef:accountName "academy" .
  ?dataset datasetDef:owner ?owner ;
           datasetDef:name ?name .
  optional { ?dataset datasetDef:license ?license }
}
order by ?name
```

### Find datasets with a tag

List all datasets tagged `geo`:

```sparql
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix datasetDef: <https://triplydb.com/Triply/triplydb/def/Dataset/>

select ?dataset ?name where {
  ?dataset datasetDef:tags/rdf:rest*/rdf:first "geo" ;
           datasetDef:name ?name .
}
```

### List saved queries and their SPARQL

List all saved queries owned by the `academy` group, with their SPARQL text and the dataset each
one runs against:

```sparql
prefix triplyDef: <https://triplydb.com/Triply/triplydb/def/>
prefix accountDef: <https://triplydb.com/Triply/triplydb/def/Account/>
prefix queryVersionDef: <https://triplydb.com/Triply/triplydb/def/QueryVersion/>

select ?name ?version ?dataset ?queryText where {
  ?query a triplyDef:QueryVersion ;
         queryVersionDef:owner/accountDef:accountName "academy" ;
         queryVersionDef:name ?name ;
         queryVersionDef:version ?version ;
         queryVersionDef:dataset ?dataset ;
         queryVersionDef:requestConfig/queryVersionDef:requestConfig\/payload/queryVersionDef:requestConfig\/payload\/query ?queryText .
}
```

### Find recent asset uploads

Show the ten most recent asset uploads, with the size of each file and who uploaded it:

```sparql
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix assetDef: <https://triplydb.com/Triply/triplydb/def/Asset/>
prefix assetVersionDef: <https://triplydb.com/Triply/triplydb/def/AssetVersion/>

select ?fileName ?size ?uploadedAt ?uploader where {
  ?asset assetDef:assetName ?fileName ;
         assetDef:versions/rdf:rest*/rdf:first ?version .
  ?version assetVersionDef:fileSize ?size ;
           assetVersionDef:uploadedAt ?uploadedAt .
  optional { ?version assetVersionDef:uploadedBy/assetVersionDef:uploadedBy\/accountName ?uploader }
}
order by desc(?uploadedAt)
limit 10
```

### Find failed flow runs

List the flow runs that failed, newest first, with the flow each belongs to and the reason it
failed:

```sparql
prefix flowDef: <https://triplydb.com/Triply/triplydb/def/Flow/>
prefix flowRunDef: <https://triplydb.com/Triply/triplydb/def/FlowRun/>

select ?flowLabel ?startedAt ?message where {
  ?run flowRunDef:status "failed" ;
       flowRunDef:createdAt ?startedAt ;
       flowRunDef:blueprint/flowDef:label ?flowLabel .
  optional { ?run flowRunDef:statusMessage ?message }
}
order by desc(?startedAt)
```

### Check the instance version

Show which TriplyDB version the instance runs, and the addresses of its console and API:

```sparql
prefix triplyDef: <https://triplydb.com/Triply/triplydb/def/>
prefix instanceDef: <https://triplydb.com/Triply/triplydb/def/Instance/>

select ?version ?consoleUrl ?apiUrl where {
  ?instance a triplyDef:Instance ;
            instanceDef:version ?version ;
            instanceDef:consoleUrl ?consoleUrl ;
            instanceDef:apiUrl ?apiUrl .
}
```

## Related

- [Instance SPARQL endpoint](https://docs.triply.cc/reference/instance-sparql-endpoint/): every class and property,
  and how resources are named
- [SPARQL introspection functions](https://docs.triply.cc/reference/introspection/): functions that tell a query
  which dataset and user it runs for
- [SPARQL: construct and federation](https://docs.triply.cc/concepts/sparql-construct-and-federation/): the `service`
  clause
