---
question: Should I import an ontology in Nexus or add it to a module?
kind: task
via_web: available
via_cli: na
via_python: available
via_api: available
via_config: na
ref_web: Ontology › Import
ref_python: ontologies/
ref_api: POST /api/ontology/import
next:
  - /capabilities/build/knowledge-graph/enable-ontology
  - /capabilities/build/knowledge-graph/creating-an-ontology
---

# Import an ontology

There are two kinds of import. They do different things:

- **A reference ontology in Nexus** (Web UI, REST API). Nexus reads the file and lists its classes and properties under References in the Ontology sidebar. The browser keeps it. The engine does not load it into the triple store, and it does not appear in Settings › Ontologies.
- **A module ontology** (Python). The file goes into a module's `ontologies/` folder. The module sends it to the schema graph `http://ontology.naas.ai/graph/schema` once it is up, and workspaces can [enable it](/capabilities/build/knowledge-graph/enable-ontology).

Use the first to browse a vocabulary next to your own. Use the second when agents, pipelines and queries should use it.

## Web UI

### Open the import screen

In the workspace, open Ontology. In its File menu, choose **Import Ontology…**. The screen reads "Upload a Turtle, OWL or RDF file to register it as a reference ontology."

### Choose the file

Drop the file on "Drop your ontology file here", or click to browse. The picker accepts `.ttl`, `.owl`, `.rdf`, `.n3` and `.nt`, but only Turtle text is read: see the warning below.

![Ontology import screen](/abi/Screenshot_Ontology_Import.png)

*Ontology › Import: drop a Turtle, OWL or RDF file, or click to browse.*

### Import it

Click **Import**. A notice gives the number of classes and properties read, and the file joins the Import History table. **Done** opens the ontology network.

:::warning What the import reads
The import matches Turtle text with two patterns, and nothing else:

- a class written with a full IRI and more statements after it: `<http://…/Item> a owl:Class ; …`
- an object property written `<http://…/hasItem> rdf:type owl:ObjectProperty`, with `rdf:type`, not `a`.

Prefixed names (`my:Item a owl:Class`), datatype properties, and RDF/XML or N-Triples files (`.owl`, `.rdf`, `.nt`) import with 0 classes and 0 properties. To load such a file for real, add it to a module (Python tab). Reference ontologies and the import history are kept in this browser's storage, not on the server.
:::

## CLI

Not applicable. There is no `abi` command that imports an ontology. To load a file into the schema graph, put it under a module's `ontologies/` folder (Python tab). To browse it in a workspace, use the Web UI. The command list is the [CLI reference](/user-guide/reference/cli).

## Python

### Add the file to a module

Copy the Turtle file under the `ontologies/` folder of a module's package, for example `src/my_module/ontologies/modules/ExternalOntology.ttl`. The module sends every `*.ttl` under `ontologies/`, recursively, the next time it starts. The file must parse as Turtle: other formats need converting first, for example with `rdflib`:

```python title="scripts/convert_to_turtle.py"
from rdflib import Graph

Graph().parse("external.owl").serialize(
    "src/my_module/ontologies/modules/ExternalOntology.ttl", format="turtle"
)
```

The module must declare the `triple_store` service, and the file name may use only letters, digits, `_`, `.` and `-`, ending in `.ttl`: otherwise the module stops at start, with a message.

### Restart the module

Once the module logs `READY`, it sends the file as `module/<module_id>/modules/ExternalOntology.ttl`. Errors are logged by `naas_abi_sdk.ontologies`. A transient failure (the engine down or busy) is retried in the background. A file the engine refuses (invalid Turtle: `INVALID_ARGUMENT`) is logged once and not sent again until the module restarts: fix the file, then restart the module. Loading is additive: removing the file later does not take its triples out of the schema graph.

### Enable it in a workspace

A file that declares `a owl:Ontology` is then in the workspace catalog, under the module's id. Turn it on in [Enable in a workspace](/capabilities/build/knowledge-graph/enable-ontology).

## REST API

### Send the file

```bash
jq -n --rawfile content external.ttl '{content: $content, filename: "external.ttl"}' |
  curl -s -X POST "http://localhost:9879/api/ontology/import" \
    -H "Authorization: Bearer $NEXUS_ACCESS_TOKEN" \
    -H "Content-Type: application/json" \
    --data-binary @-
```

`jq` builds the JSON body and pipes it to `curl`, so a large file does not hit the shell's limit on argument size.

`content` is the file text (up to 5,000,000 characters). `filename` is a plain file name with an extension. The token is a Nexus login token: see [Enable in a workspace](/capabilities/build/knowledge-graph/enable-ontology?via=api) for the sign-in call.

### Read the result

The response is the parsed reference ontology: `id`, `name`, `file_path`, `format`, `imported_at`, and its `classes` and `properties` with their IRIs, labels and definitions. The server keeps nothing: the Web UI stores this response in the browser. The same reading rules apply as in the Web UI warning.

## Config

Not applicable. No `config.yaml` key loads a Turtle file from outside a module. A file is loaded because a module sends it from its `ontologies/` folder. A workspace's `ontologies:` list only picks from those files: see [Enable in a workspace](/capabilities/build/knowledge-graph/enable-ontology?via=config).
