# Server client - Python

> agdb example: managing users and databases and running queries on agdb_server from Python

## What it shows

The Python version of the server client example using the `agdb_api` Python package. It checks the server status, logs in as the admin, creates a user and a memory mapped database and runs all queries in a single batch, linking the inserted nodes with references to the results of earlier queries in the same batch (`:0`, `:1`).

## Run it

From the repository root (requires [uv](https://docs.astral.sh/uv/)):

```bash
pnpm run examples --filter agdb_api_python
```

The script builds the `agdb_server` and runs the example against a freshly started server. The port 3000 must be free (stop any `agdb_server` you started yourself). Against another server set the `AGDB_SERVER_ADDRESS` environment variable (default `localhost:3000`).

To use the client in your own project, install it from the repository:

```bash
pip install "git+https://github.com/agnesoft/agdb#subdirectory=agdb_api/python"
```

See the [Python client](/api-docs/python) page for details.

## Users and databases

```python
import os

from agdb_api import AgdbApi, DbKind, QueryBuilder, UrllibClient

# Create the client using the `urllib` transport of the standard library.
client = AgdbApi.new(
    UrllibClient(), os.environ.get("AGDB_SERVER_ADDRESS", "localhost:3000")
)

# Runs the status query against the server and raises if it is not
# accessible.
client.status()

# Login as server admin.
client.user_login("admin", "admin")

# Create user "py_user1".
client.admin_user_add("py_user1", "py_user1")

# Login as "py_user1".
client.user_login("py_user1", "py_user1")

# Creates memory mapped database "db1" for user "py_user1".
client.db_add("py_user1", "db1", DbKind.Mapped)
```

## One batch with result references

Within a batch a query can refer to the result of an earlier query by its index prefixed with a colon. The edges are inserted from the result of query `:0` (the `users` root) to the result of query `:1` (the user nodes):

```python
queries = [
    # :0: Inserts a root node aliased "users".
    QueryBuilder.insert().nodes().aliases(["users"]).query(),
    # :1: Inserts more nodes with some data.
    QueryBuilder.insert()
    .nodes()
    .values(
        [
            {"username": "user1", "password": "password123"},
            {"username": "user2", "password": "password456"},
        ]
    )
    .query(),
    # :2: Connect the root to the inserted nodes with edges referencing
    # both from previous queries.
    QueryBuilder.insert().edges().from_(":0").to(":1").query(),
    # :3: Find a node starting at the "users" node with specific username.
    QueryBuilder.select()
    .search()
    .from_("users")
    .where()
    .key("username")
    .value("user1")
    .query(),
]

# Execute the queries.
_, results = client.db_exec_mut("py_user1", "db1", queries)

# Print the result of the last query.
user = results[3].elements[0]
print(f"User (id: {user.id})")

for kv in user.values:
    print(f"{kv.key.to_json()}: {kv.value.to_json()}")
```

The client returns the HTTP status and the list of results, one per query, so `results[3]` is the result of the search. Names that are Python keywords get a trailing underscore (`from_`).

## Learn more

- [Python client](/api-docs/python) - installing and using the `agdb_api` Python package.
- [Server reference](/docs/references/server)

<style>

html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}

</style>

---

- [View source](https://github.com/agnesoft/agdb/tree/main/agdb_api/python/examples/server_client)
