This document provides detailed examples showing how to use LARS and how it works internally.
curl -X POST http://localhost:8000/products \
-H "Content-Type: application/json" \
-d '{"name": "Laptop", "price": 1299.99, "stock": 15}'Response (201 Created):
{
"id": "1",
"name": "Laptop",
"price": 1299.99,
"stock": 15
}Response Headers:
Location: /products/1
Content-Type: application/json
curl http://localhost:8000/products/1Response (200 OK):
{
"id": "1",
"name": "Laptop",
"price": 1299.99,
"stock": 15
}curl http://localhost:8000/products?limit=10&offset=0Response (200 OK):
{
"items": [
{"id": "1", "name": "Laptop", "price": 1299.99, "stock": 15},
{"id": "2", "name": "Mouse", "price": 29.99, "stock": 100}
],
"page": {
"limit": 10,
"offset": 0,
"total": 2
}
}curl "http://localhost:8000/products/search?name=Laptop"Response (200 OK):
[
{"id": "1", "name": "Laptop", "price": 1299.99, "stock": 15}
]curl -X PATCH http://localhost:8000/products/1 \
-H "Content-Type: application/json" \
-d '{"stock": 10}'Response (200 OK):
{
"id": "1",
"name": "Laptop",
"price": 1299.99,
"stock": 10
}curl -X PUT http://localhost:8000/products/1 \
-H "Content-Type: application/json" \
-d '{"name": "Gaming Laptop", "price": 1599.99, "stock": 8}'Response (200 OK):
{
"id": "1",
"name": "Gaming Laptop",
"price": 1599.99,
"stock": 8
}curl -X DELETE http://localhost:8000/products/1Response (204 No Content): (Empty body)
Let's trace what happens when you make a request to create a product.
curl -X POST http://localhost:8000/products \
-H "Content-Type: application/json" \
-d '{"name": "Laptop", "price": 1299.99}'FastAPI receives the request and app/api.py builds a context dictionary:
{
"method": "POST",
"path": "/products",
"segments": ["products"],
"query": {},
"headers": {
"Content-Type": "application/json",
"Host": "localhost:8000"
},
"body_json": {
"name": "Laptop",
"price": 1299.99
},
"client": {"ip": "127.0.0.1"},
"session": {"id": "sess_abc123", "token": "..."},
"request_id": "req_xyz789"
}The sandbox's router.py builds a detailed prompt:
You are a REST Planner. Analyze the HTTP request and return ONLY a JSON object (no other text).
The JSON MUST have these exact fields:
- "action": one of "create", "get", "list", "replace", "patch", "delete", or "search"
- "resource": the collection name (first path segment)
- "identifier": the ID from path (or null)
- "criteria": {} (empty object)
- "payload": the request body (or {})
- "response_hints": {}
- "code": {"language": "python", "block": "..."}
The code block must:
- Use `store` (ResourceStore API), `ctx` (request context), `plan`, and `make_response(status, body, headers)`
- Set a variable called `REPLY` using make_response()
- `make_response` signature: `make_response(status: int, body=None, headers=None)` (ONLY these 3 args)
- For POST /resource/ → `body = ctx.get("body_json"); rec = store.insert(body); REPLY = make_response(201, rec, {"Location": f"/resource/{rec['id']}"})`
**REQUEST CONTEXT:**
{
"method": "POST",
"path": "/products",
"segments": ["products"],
"body_json": {
"name": "Laptop",
"price": 1299.99
},
...
}
**Now output only the JSON object per the schema above.**
The LLM (OpenAI or Anthropic) analyzes the request and returns:
{
"action": "create",
"resource": "products",
"identifier": null,
"criteria": {},
"payload": {
"name": "Laptop",
"price": 1299.99
},
"response_hints": {},
"code": {
"language": "python",
"block": "body = ctx.get(\"body_json\")\nif not isinstance(body, dict):\n raise ValueError(\"Expected JSON object body\")\nrecord = store.insert(dict(body))\nlocation = f\"/products/{record['id']}\"\nheaders = {\"Content-Type\": \"application/json\", \"Location\": location}\nREPLY = make_response(201, record, headers=headers)"
}
}The router.py extracts the Python code from the plan:
body = ctx.get("body_json")
if not isinstance(body, dict):
raise ValueError("Expected JSON object body")
record = store.insert(dict(body))
location = f"/products/{record['id']}"
headers = {"Content-Type": "application/json", "Location": location}
REPLY = make_response(201, record, headers=headers)The sandbox's safety.py validates the code:
- Parse into AST
- Check each node against allowlist (no imports, no file I/O, etc.)
- Reject any disallowed constructs
- Allow the code to proceed
Allowed nodes include: assignments, function calls, comparisons, f-strings, loops, conditionals Disallowed nodes include: imports, lambdas, class/function definitions, file operations
The sandbox executes the validated code:
# Available in execution context:
# - store: ResourceStore instance for this session
# - ctx: Request context dict
# - plan: Plan object from LLM
# - make_response: Function to build response
# Execution happens in restricted namespace:
sandbox_globals = {
"__builtins__": {
"len": len, "range": range, "dict": dict,
"str": str, "int": int, ...
},
"store": <ResourceStore>,
"ctx": <RequestContext>,
"plan": <Plan>,
"make_response": <function>
}
exec(compiled_code, sandbox_globals, sandbox_locals)
# After execution:
# sandbox_locals["REPLY"] = {
# "status": 201,
# "body": {"id": "1", "name": "Laptop", "price": 1299.99},
# "headers": {"Content-Type": "application/json", "Location": "/products/1"},
# "is_json": True
# }During execution, store.insert() is called:
- Generates ID:
"1" - Adds ID to record:
{"id": "1", "name": "Laptop", "price": 1299.99} - Stores in memory:
self._data["products"]["1"] = {...} - Persists to JSON file:
data/sessions/sess_abc123.json - Returns the record with ID
File contents after insert:
{
"products": {
"1": {
"id": "1",
"name": "Laptop",
"price": 1299.99
}
}
}The API process receives the sandbox response and builds the HTTP response:
# Sandbox returned:
{
"status": 201,
"body": {"id": "1", "name": "Laptop", "price": 1299.99},
"headers": {"Location": "/products/1"},
"is_json": True
}
# FastAPI sends:
HTTP/1.1 201 Created
Content-Type: application/json
Location: /products/1
X-Request-ID: req_xyz789
{
"id": "1",
"name": "Laptop",
"price": 1299.99
}curl -X POST http://localhost:8000/posts \
-H "Content-Type: application/json" \
-d '{
"title": "Getting Started with LARS",
"author": "Alice",
"content": "LARS is a dynamic REST API powered by LLMs...",
"tags": ["tutorial", "rest", "llm"]
}'Response:
{
"id": "1",
"title": "Getting Started with LARS",
"author": "Alice",
"content": "LARS is a dynamic REST API powered by LLMs...",
"tags": ["tutorial", "rest", "llm"]
}curl -X POST http://localhost:8000/comments \
-H "Content-Type: application/json" \
-d '{
"post_id": "1",
"author": "Bob",
"text": "Great article!"
}'Response:
{
"id": "1",
"post_id": "1",
"author": "Bob",
"text": "Great article!"
}curl "http://localhost:8000/posts/search?author=Alice"Response:
[
{
"id": "1",
"title": "Getting Started with LARS",
"author": "Alice",
"content": "LARS is a dynamic REST API powered by LLMs...",
"tags": ["tutorial", "rest", "llm"]
}
]# Add laptop
curl -X POST http://localhost:8000/inventory \
-H "Content-Type: application/json" \
-d '{"sku": "LAP-001", "name": "Gaming Laptop", "quantity": 5, "price": 1599.99}'
# Add mouse
curl -X POST http://localhost:8000/inventory \
-H "Content-Type: application/json" \
-d '{"sku": "MOU-001", "name": "Wireless Mouse", "quantity": 50, "price": 29.99}'curl "http://localhost:8000/inventory/search?sku=LAP-001"Response:
[
{
"id": "1",
"sku": "LAP-001",
"name": "Gaming Laptop",
"quantity": 5,
"price": 1599.99
}
]curl -X PATCH http://localhost:8000/inventory/1 \
-H "Content-Type: application/json" \
-d '{"quantity": 4}'Response:
{
"id": "1",
"sku": "LAP-001",
"name": "Gaming Laptop",
"quantity": 4,
"price": 1599.99
}curl -X POST http://localhost:8000/projects \
-H "Content-Type: application/json" \
-d '{
"name": "Website Redesign",
"status": "active",
"owner": "alice@example.com"
}'curl -X POST http://localhost:8000/tasks \
-H "Content-Type: application/json" \
-d '{
"project_id": "1",
"title": "Design mockups",
"assignee": "bob@example.com",
"status": "todo",
"priority": "high"
}'
curl -X POST http://localhost:8000/tasks \
-H "Content-Type: application/json" \
-d '{
"project_id": "1",
"title": "Implement header component",
"assignee": "carol@example.com",
"status": "in_progress",
"priority": "medium"
}'curl "http://localhost:8000/tasks?limit=20"Response:
{
"items": [
{
"id": "1",
"project_id": "1",
"title": "Design mockups",
"assignee": "bob@example.com",
"status": "todo",
"priority": "high"
},
{
"id": "2",
"project_id": "1",
"title": "Implement header component",
"assignee": "carol@example.com",
"status": "in_progress",
"priority": "medium"
}
],
"page": {
"limit": 20,
"offset": 0,
"total": 2
}
}curl "http://localhost:8000/tasks/search?priority=high"Response:
[
{
"id": "1",
"project_id": "1",
"title": "Design mockups",
"assignee": "bob@example.com",
"status": "todo",
"priority": "high"
}
]# Get first page (10 items)
curl "http://localhost:8000/products?limit=10&offset=0"
# Get second page
curl "http://localhost:8000/products?limit=10&offset=10"
# Get all items (no limit)
curl "http://localhost:8000/products"# Search by multiple fields
curl "http://localhost:8000/products/search?category=electronics&price_min=100"Note: The LLM will interpret natural search parameters. For more complex queries, the generated code might include comparisons:
# LLM might generate code like:
query = ctx.get("query") or {}
criteria = {}
for key, values in query.items():
if key == "price_min":
# LLM could generate comparison logic
continue
criteria[key] = values[-1] if values else None
matches = []
for item in store.list()[0]:
if all(item.get(k) == v for k, v in criteria.items() if v):
if "price_min" in query:
price_min = int(query["price_min"][0])
if item.get("price", 0) >= price_min:
matches.append(item)
else:
matches.append(item)
REPLY = make_response(200, matches)curl http://localhost:8000/products/999Response (404 Not Found):
{
"error": "not found"
}curl -X POST http://localhost:8000/products \
-H "Content-Type: application/json" \
-d '{invalid json}'Response (400 Bad Request):
{
"detail": "Invalid JSON in request body"
}Each session has isolated storage:
Session A:
curl -X POST http://localhost:8000/notes \
-H "X-Session-Token: session-a-token" \
-H "Content-Type: application/json" \
-d '{"text": "Session A note"}'
curl http://localhost:8000/notes \
-H "X-Session-Token: session-a-token"
# Returns: [{"id": "1", "text": "Session A note"}]Session B:
curl http://localhost:8000/notes \
-H "X-Session-Token: session-b-token"
# Returns: {"items": [], "page": {...}} (empty - different session)With authentication enabled:
# Without token (rejected)
curl http://localhost:8000/products
# Response: 401 Unauthorized
# With valid token
curl http://localhost:8000/products \
-H "Authorization: Bearer your-secret-token"
# Response: 200 OK with dataThe LLM interprets requests dynamically, which means:
-
Same request, different code: Two identical POST requests might generate slightly different Python code, but both will work correctly.
-
Adaptive behavior: The LLM can handle variations in request structure:
# Both of these work: curl -X POST http://localhost:8000/users -d '{"name": "Alice"}' curl -X POST http://localhost:8000/user -d '{"username": "Alice"}'
-
Natural language in paths: The LLM can interpret semantic variations:
# These might be interpreted similarly: GET /products/search?name=Laptop GET /products/find?name=Laptop GET /products/query?name=Laptop
Set logging to DEBUG to see the prompts sent to the LLM:
# In your environment or config
import logging
logging.getLogger("sandbox_runtime.router").setLevel(logging.DEBUG)The sandbox logs the generated code before execution. Check logs for:
[sandbox_runtime.runtime] Executing plan code for action=create
Use the mock mode in tests:
export LLM_MOCK_HANDLER="tests.conftest._mock_llm_planner"
pytest tests/This uses deterministic code generation for predictable testing.
LARS provides a unique approach to REST APIs:
- No endpoint definitions needed - Just send requests and the LLM figures out what to do
- Sandbox security - All untrusted code runs in isolated environment
- Session isolation - Each session has its own data store
- RESTful semantics - Proper HTTP methods and status codes
- LLM-powered flexibility - Handles variations in request structure naturally
For more details, see the main README.md.