> ## Documentation Index
> Fetch the complete documentation index at: https://docs.graphora.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Ontology Examples

> Examples of working with ontologies in Graphora

# Ontology Examples

This page provides practical examples of working with ontologies in the Graphora client library.

## Creating and Registering an Ontology

The following example demonstrates how to create an ontology definition and register it with Graphora:

```python theme={null}
import os
from graphora import GraphoraClient

client = GraphoraClient(
    auth_token=os.environ["GRAPHORA_AUTH_TOKEN"],
)

# Define your ontology
ontology_yaml = """
version: 1
entities:
  Person:
    properties:
      name:
        type: string
        unique: true
        required: true
      age:
        type: integer
      email:
        type: string
        index: true
    relationships:
      works_at:
        target: Company
        cardinality: many_to_one
      knows:
        target: Person
        cardinality: many_to_many
  
  Company:
    properties:
      name:
        type: string
        unique: true
        required: true
      industry:
        type: string
      founded_year:
        type: integer
    relationships:
      has_subsidiary:
        target: Company
        cardinality: one_to_many
"""

# Register the ontology
response = client.register_ontology(ontology_yaml)
print(f"Ontology registered with ID: {response.id}")
```

## Loading an Ontology from a File

You can also load an ontology from a YAML file:

```python theme={null}
import os
from graphora import GraphoraClient

client = GraphoraClient(
    auth_token=os.environ["GRAPHORA_AUTH_TOKEN"],
)

# Load the ontology from a file
with open("ontology.yaml", "r") as f:
    ontology_yaml = f.read()

# Register the ontology
response = client.register_ontology(ontology_yaml)
print(f"Ontology registered with ID: {response.id}")
```

## Retrieving an Existing Ontology

You can retrieve a previously registered ontology by its ID:

```python theme={null}
import os
from graphora import GraphoraClient

client = GraphoraClient(
    auth_token=os.environ["GRAPHORA_AUTH_TOKEN"],
)

# Retrieve an ontology by ID
ontology_id = "your-ontology-id"
ontology_yaml = client.get_ontology(ontology_id)
print("Retrieved ontology:")
print(ontology_yaml)
```

## Updating an Existing Ontology

To update an existing ontology, you can use the same `register_ontology` method with an updated definition:

```python theme={null}
import os
from graphora import GraphoraClient

client = GraphoraClient(
    auth_token=os.environ["GRAPHORA_AUTH_TOKEN"],
)

# Define your updated ontology
updated_ontology_yaml = """
version: 2
entities:
  Person:
    properties:
      name:
        type: string
        unique: true
        required: true
      age:
        type: integer
      email:
        type: string
        index: true
      phone:  # New property added
        type: string
    relationships:
      works_at:
        target: Company
        cardinality: many_to_one
      knows:
        target: Person
        cardinality: many_to_many
  
  Company:
    properties:
      name:
        type: string
        unique: true
        required: true
      industry:
        type: string
      founded_year:
        type: integer
      website:  # New property added
        type: string
    relationships:
      has_subsidiary:
        target: Company
        cardinality: one_to_many
"""

# Register the updated ontology
response = client.register_ontology(updated_ontology_yaml)
print(f"Updated ontology registered with ID: {response.id}")
```

These examples demonstrate the basic operations you can perform with ontologies in the Graphora client library. For more detailed information about ontology structure and options, see the [Ontology Concepts](/client/concepts/ontology) page.
