---
title: API Integration
sidebar_position: 6
description: Call the Django REST API from Streamlit using AdminAPI — auto-generated typed Python clients.
---

# API Integration

Streamlit Admin ships with `AdminAPI`, a wrapper around auto-generated Python REST clients. You call Django API endpoints from Streamlit using typed Python methods — no manual HTTP requests.

---

## Basic Usage

```python
from api.client import AdminAPI

api = AdminAPI()

# Get current user profile
profile = api.cfg_user_profile.accounts_profile_retrieve()

# List RQ queues
queues = api.cfg_rq_queues.list()
```

### Context Manager

```python
with AdminAPI() as api:
    workers = api.cfg_rq_workers.list()
    jobs = api.cfg_rq_jobs.list()
```

---

## Available Clients

`AdminAPI` exposes generated clients for all configured Django API modules:

| Client | Description |
|--------|-------------|
| `api.cfg_user_profile` | User profile operations |
| `api.cfg_rq_queues` | RQ queue management |
| `api.cfg_rq_workers` | RQ worker info |
| `api.cfg_rq_jobs` | RQ job management |
| `api.cfg_centrifugo` | Centrifugo WebSocket stats |
| `api.cfg_system` | System metrics (CPU, memory, disk) |

The exact clients depend on which modules you have enabled in `DjangoConfig`.

---

## Generate Clients

When you add or change API endpoints, regenerate the Python clients:

```bash
python manage.py generate_client
```

This reads your OpenAPI schema and writes typed client code to `api_clients_path` (default: `streamlit/generated/`).

Commit the generated files — they're stable, versioned code.

---

## Authentication

`AdminAPI` automatically uses the current authenticated user's JWT token, obtained from Streamlit's session state (set by `require_auth()`).

You don't need to pass auth headers manually.

---

## Error Handling

```python
from api.client import AdminAPI
import streamlit as st

api = AdminAPI()

try:
    queues = api.cfg_rq_queues.list()
    for q in queues.results:
        st.metric(q.name, q.job_count)
except Exception as e:
    st.error(f"Failed to load queues: {e}")
```

---

## Example: System Dashboard

```python
import streamlit as st
from api.auth import require_auth
from api.client import AdminAPI
from django_cfg.modules.streamlit_admin import page_registry

@page_registry.register("System", icon="cpu", group="Monitoring", order=10)
def render_system():
    require_auth()
    st.title("System Metrics")

    api = AdminAPI()
    metrics = api.cfg_system.retrieve()

    col1, col2, col3 = st.columns(3)
    with col1:
        st.metric("CPU", f"{metrics.cpu_percent:.1f}%")
    with col2:
        st.metric("Memory", f"{metrics.memory_percent:.1f}%")
    with col3:
        st.metric("Disk", f"{metrics.disk_percent:.1f}%")
```

---

## API Config

The base URL for API calls is configured in `streamlit/api/config.py` (generated by scaffold):

```python
# streamlit/api/config.py
import os

API_BASE_URL = os.environ.get(
    "DJANGO_API_URL",
    "http://localhost:8000/api",
)
```

Override via environment variable or update this file directly for your project.
