---
question: How do I add triples from a pipeline or the Knowledge Graph screen?
kind: task
via_web: available
via_cli: na
via_python: available
via_api: available
via_config: na
ref_web: Knowledge Graph › File › Import Triples…
ref_python: triple_store.insert
ref_api: POST /api/graph/import
next:
  - /capabilities/build/knowledge-graph/explore-the-graph
  - /capabilities/build/knowledge-graph/creating-a-pipeline
---

# Add data

Triples go into a named graph. In Nexus, a member imports a file or creates an individual by hand, in a graph the workspace can edit. In code, a pipeline builds an `rdflib.Graph` and inserts it.

## Web UI

### Create a graph, if you need one

In the Nexus workspace, open **Knowledge Graph**. Choose **File › Create New Graph**. Fill **Label** and **Role**, and optionally **Description**, then save. The workspace needs **Allow members to create graphs** in [Manage graphs](/user-guide/build/knowledge-graph/manage-graphs).

### Import a file

Choose **File › Import Triples…** and pick a `.ttl`, `.owl`, `.nt`, `.rdf` or `.n3` file. Nexus analyses it and shows the counts. Under **Graph destination**, choose **Create new graph** or **Import into existing graph**, then **Import**.

![Knowledge Graph import screen](/abi/Screenshot_Graph_Import.png)

*Knowledge Graph › File › Import Triples…: drop a Turtle, OWL or N-Triples file.*

:::note
An import keeps only the triples whose subject is an `owl:NamedIndividual`. Class and property definitions belong in an ontology: see [Import an ontology](/user-guide/build/knowledge-graph/import-an-ontology).
:::

### Or create one individual

Choose **File › Create New Instance**. Fill **Label**, pick the **Graph** (writable graphs only) and, optionally, a **Class**. Add **Properties** and **Relations**, then **Create Individual**.

Super admins can also write a whole named graph as Turtle under System › Data › Knowledge graph › **New graph**.

## CLI

Not applicable. There is no `abi` command that writes triples. Use the Python steps on this page, or a pipeline. The command list is the [CLI reference](/user-guide/reference/cli).

## Python

### Declare the service

```python title="src/my_module/__init__.py"
from naas_abi_core.module.Module import BaseModule, ModuleDependencies
from naas_abi_core.services.triple_store.TripleStoreService import TripleStoreService


class ABIModule(BaseModule):
    dependencies: ModuleDependencies = ModuleDependencies(
        modules=[],
        services=[TripleStoreService],
    )
```

### Build and insert the triples

```python
from rdflib import RDF, Graph, Literal, Namespace, URIRef
from rdflib.namespace import OWL, RDFS

GRAPH = URIRef("http://ontology.naas.ai/graph/my_module")
EX = Namespace("http://ontology.naas.ai/my_module/")

graph = Graph()
graph.add((EX.acme, RDF.type, OWL.NamedIndividual))
graph.add((EX.acme, RDFS.label, Literal("Acme")))

self.engine.services.triple_store.insert(graph, GRAPH)
```

`remove(graph, GRAPH)` takes the same arguments and deletes those triples. `create_graph`, `clear_graph` and `drop_graph` manage the named graph itself. A class that fetches, builds and inserts on a schedule is a pipeline: see [Create a pipeline](/user-guide/build/knowledge-graph/creating-a-pipeline).

## REST API

The graph routes of the Nexus API. Auth: a Nexus session of a workspace member (`POST /api/auth/login` returns the access token). The workspace must be allowed to edit the target graph.

### Import a file

```bash
curl -X POST "http://localhost:9879/api/graph/import" \
  -H "Authorization: Bearer $NEXUS_TOKEN" \
  -F "workspace_id=$WORKSPACE_ID" \
  -F "graph_uri=http://ontology.naas.ai/graph/my_module" \
  -F "file=@individuals.ttl"
```

The answer is `{"status": "imported", "count": N}`. The format follows the file extension. With `abi dev up`, use the `api` port that `abi dev ports` prints.

### Or create a graph and an individual

```http
POST /api/graph/create
POST /api/graph/nodes
```

`create` takes `{"workspace_id", "label", "description", "role_label"}` and returns the graph (`id`, `uri`, `label`, `role_label`, `can_write`): its IRI is `uri`. `nodes` takes `{"workspace_id", "graph_uri", "label", "class_uri", "properties", "relations"}`.

## Config

Not applicable. `config.yaml` picks the triple store, not its triples. Ontologies load from each module's `ontologies/` folder, not from `config.yaml`.
