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

# API Server

> Expose your chatbot as a REST API with a built-in web UI.

## Minimal server

```python theme={null}
# server.py
from langchat import LangChat
from langchat.api import create_app
from langchat.providers import OpenAI, Pinecone, Supabase
import uvicorn

LangChat.load_env()

app = create_app(
    llm=OpenAI("gpt-4o-mini"),
    vector_db=Pinecone("my-index"),
    db=Supabase(),
)

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)
```

```bash theme={null}
python server.py
```

***

## Endpoints

| Method | Path         | Description                               |
| ------ | ------------ | ----------------------------------------- |
| `GET`  | `/`          | Redirects to `/frontend/`                 |
| `GET`  | `/health`    | Health check — returns status and version |
| `GET`  | `/frontend/` | Built-in chat UI (served from Vite build) |
| `POST` | `/chat`      | Send a message                            |

### POST /chat

Accepts `multipart/form-data`:

| Field      | Type     | Required | Description                       |
| ---------- | -------- | -------- | --------------------------------- |
| `query`    | `string` | ✓        | User message                      |
| `userId`   | `string` | ✓        | User identifier                   |
| `platform` | `string` | ✓        | Platform namespace (e.g. `"web"`) |
| `image`    | `file`   | —        | Optional image attachment         |

**Example with curl:**

```bash theme={null}
curl -X POST http://localhost:8000/chat \
  -F "query=What is our return policy?" \
  -F "userId=alice" \
  -F "platform=web"
```

**Response:**

```json theme={null}
{
  "response": "You can return items within 30 days...",
  "user_id": "alice",
  "timestamp": "2025-01-15T10:30:00.123Z",
  "status": "success",
  "response_time": 1.23
}
```

### GET /health

```bash theme={null}
curl http://localhost:8000/health
```

```json theme={null}
{
  "status": "healthy",
  "timestamp": "2025-01-15T10:30:00.123Z",
  "version": "1.0.1"
}
```

***

## Custom prompt in API server

```python theme={null}
app = create_app(
    llm=OpenAI("gpt-4o-mini"),
    vector_db=Pinecone("my-index"),
    db=Supabase(),
    prompt_template="""You are Aria, support agent for Acme Corp.

Context:
{context}

History:
{chat_history}

Customer: {question}
Aria:""",
)
```

***

## Production server

For production, disable auto-reload and run multiple workers:

```python theme={null}
# server.py
from langchat import LangChat
from langchat.api import create_app
from langchat.providers import OpenAI, Pinecone, Supabase

LangChat.load_env()

app = create_app(
    llm=OpenAI("gpt-4o-mini"),
    vector_db=Pinecone("my-index"),
    db=Supabase(),
    max_chat_history=20,
)
```

```bash theme={null}
# Production: multiple workers, no reload
uvicorn server:app --host 0.0.0.0 --port 8000 --workers 4
```

***

## Frontend build

The built-in UI is served from `src/langchat/core/ui/dist/`. Build it with:

```bash theme={null}
cd src/langchat/core/ui
npm install
npm run build
```

The UI is then available at `http://localhost:8000/frontend/`.

***

## Docker

LangChat can auto-generate a Dockerfile:

```python theme={null}
app = create_app(
    llm=OpenAI("gpt-4o-mini"),
    vector_db=Pinecone("my-index"),
    db=Supabase(),
    auto_generate_docker=True,   # generates Dockerfile + .dockerignore
)
```

Or write your own:

```dockerfile theme={null}
FROM python:3.11-slim

WORKDIR /app
COPY . .

RUN pip install langchat uvicorn

ENV OPENAI_API_KEY=""
ENV PINECONE_API_KEY=""
ENV SUPABASE_URL=""
ENV SUPABASE_KEY=""

EXPOSE 8000
CMD ["uvicorn", "server:app", "--host", "0.0.0.0", "--port", "8000"]
```

```bash theme={null}
docker build -t my-chatbot .
docker run -p 8000:8000 \
  -e OPENAI_API_KEY=sk-... \
  -e PINECONE_API_KEY=pcsk-... \
  -e SUPABASE_URL=https://xxxx.supabase.co \
  -e SUPABASE_KEY=eyJ... \
  my-chatbot
```

***

## Adding custom routes

Inject your own FastAPI routes alongside LangChat's:

```python theme={null}
from fastapi import APIRouter
from langchat.api import create_app

my_router = APIRouter()

@my_router.get("/status")
async def status():
    return {"my_service": "running"}

@my_router.post("/feedback")
async def feedback(message_id: str, rating: int):
    # save feedback to your DB
    return {"saved": True}

app = create_app(
    llm=OpenAI("gpt-4o-mini"),
    vector_db=Pinecone("my-index"),
    db=Supabase(),
    custom_routes=[my_router],
)
```
