misc / mem0 /docs /open-source /features /rest-api.mdx
NingsenWang's picture
Upload mem0 project snapshot
0ae3f27 verified
---
title: REST API Server
description: Reach every Mem0 OSS capability through a FastAPI-powered REST layer.
icon: "code"
---
The Mem0 REST API server exposes every OSS memory operation over HTTP. Run it alongside your stack to add, search, update, and delete memories from any language that speaks REST.
<Info>
**You’ll use this when…**
- Your services already talk to REST APIs and you want Mem0 to match that style.
- Teams on languages without the Mem0 SDK still need access to memories.
- You plan to explore or debug endpoints through the built-in OpenAPI page at `/docs`.
</Info>
<Warning>
**OSS vs Platform API paths:** The self-hosted OSS server does **not** use the `/v1/` prefix. For example, the endpoint is `POST /memories`, not `POST /v1/memories/`. The [API Reference](/api-reference) documents the hosted platform at `api.mem0.ai` which uses `/v1/` paths — those do not apply to the OSS server.
</Warning>
<Warning>
Enable API key authentication (see below) and HTTPS before exposing the server to anything beyond your internal network.
</Warning>
---
## Feature
- **CRUD endpoints:** Create, retrieve, search, update, delete, and reset memories by `user_id`, `agent_id`, or `run_id`.
- **API key authentication:** Optionally secure all endpoints with a shared API key via the `X-API-Key` header.
- **Status health check:** Access base routes to confirm the server is online.
- **OpenAPI explorer:** Visit `/docs` for interactive testing and schema reference.
---
## Configure it
### Run with Docker Compose (development)
<Tabs>
<Tab title="Steps">
1. Create `server/.env` with your keys:
```bash
OPENAI_API_KEY=your-openai-api-key
```
2. Start the stack:
```bash
cd server
docker compose up
```
3. Reach the API at `http://localhost:8888`. Edits to the server or library auto-reload.
</Tab>
</Tabs>
### Run with Docker
<Tabs>
<Tab title="Pull image">
```bash
docker pull mem0/mem0-api-server
```
</Tab>
<Tab title="Build locally">
```bash
docker build -t mem0-api-server .
```
</Tab>
</Tabs>
1. Create a `.env` file with `OPENAI_API_KEY`.
2. Run the container:
```bash
docker run -p 8000:8000 --env-file .env mem0-api-server
```
3. Visit `http://localhost:8000`.
### Run directly (no Docker)
```bash
pip install -r requirements.txt
uvicorn main:app --reload
```
<Tip>
Use a process manager such as `systemd`, Supervisor, or PM2 when deploying the FastAPI server for production resilience.
</Tip>
<Note>
The REST server reads the same configuration you use locally, so you can point it at your preferred LLM, vector store, graph backend, and reranker without changing code.
</Note>
---
## Authentication
The server supports optional API key authentication. When the `ADMIN_API_KEY` environment variable is set, every endpoint requires a valid `X-API-Key` header. The `/` redirect, `/docs`, and `/openapi.json` routes remain open so you can always reach the interactive API explorer.
| `ADMIN_API_KEY` value | Behavior |
|---|---|
| Not set / empty | All endpoints are open (no auth) |
| Any non-empty string | Requests must include `X-API-Key: <your-key>` |
### Enable authentication
Add the key to your `.env` file:
```bash
ADMIN_API_KEY=your-secret-api-key
```
Then include the header in every request:
```bash
curl -X POST http://localhost:8000/memories \
-H "Content-Type: application/json" \
-H "X-API-Key: your-secret-api-key" \
-d '{
"messages": [{"role": "user", "content": "I love pizza."}],
"user_id": "alice"
}'
```
<Warning>
The server logs a warning at startup when `ADMIN_API_KEY` is not set. Always set it in production.
</Warning>
---
## See it in action
### Create and search memories via HTTP
```bash
curl -X POST http://localhost:8000/memories \
-H "Content-Type: application/json" \
-d '{
"messages": [
{"role": "user", "content": "I love fresh vegetable pizza."}
],
"user_id": "alice"
}'
```
<Info icon="check">
Expect a JSON response containing the new memory IDs and events (`ADD`, etc.).
</Info>
```bash
curl -X POST http://localhost:8000/search \
-H "Content-Type: application/json" \
-d '{
"query": "vegetable",
"user_id": "alice"
}'
```
### Explore with OpenAPI docs
1. Navigate to `http://localhost:8000/docs`.
2. Pick an endpoint (e.g., `POST /search`).
3. Fill in parameters and click **Execute** to try requests in-browser.
<Tip>
Export the generated `curl` snippets from the OpenAPI UI to bootstrap integration tests.
</Tip>
---
## Endpoint reference
The OSS REST server exposes the following endpoints. None use the `/v1/` prefix.
| Method | Path | Description |
|--------|------|-------------|
| `POST` | `/configure` | Set memory configuration |
| `POST` | `/memories` | Create memories |
| `GET` | `/memories` | Get all memories (filter by `user_id`, `agent_id`, or `run_id`) |
| `GET` | `/memories/{memory_id}` | Get a specific memory |
| `PUT` | `/memories/{memory_id}` | Update a memory |
| `DELETE` | `/memories/{memory_id}` | Delete a specific memory |
| `DELETE` | `/memories` | Delete all memories for an identifier |
| `GET` | `/memories/{memory_id}/history` | Get memory history |
| `POST` | `/search` | Search memories |
| `POST` | `/reset` | Reset all memories |
---
## Verify the feature is working
- Hit the root route and `/docs` to confirm the server is reachable.
- Run a full cycle: `POST /memories``GET /memories/{id}``DELETE /memories/{id}`.
- Watch server logs for import errors or provider misconfigurations during startup.
- Confirm environment variables (API keys, vector store credentials) load correctly when containers restart.
---
## Best practices
1. **Enable authentication:** Set `ADMIN_API_KEY` to secure all endpoints, or use an API gateway for more advanced schemes.
2. **Use HTTPS:** Terminate TLS at your load balancer or reverse proxy.
3. **Monitor uptime:** Track request rates, latency, and error codes per endpoint.
4. **Version configs:** Keep environment files and Docker Compose definitions in source control.
5. **Limit exposure:** Bind to private networks unless you explicitly need public access.
---
<CardGroup cols={2}>
<Card title="Configure OSS Components" icon="sliders" href="/open-source/configuration">
Fine-tune LLMs, vector stores, and graph backends that power the REST server.
</Card>
<Card title="Automate Agent Integrations" icon="plug" href="/cookbooks/integrations/agents-sdk-tool">
See how services call the REST endpoints as part of an automation pipeline.
</Card>
</CardGroup>