This directory contains examples for using the undatum api commands to expose data files as HTTP APIs.
The simplest way to expose a data file as an API:
undatum api run data-20130920T1245.jsonlThis will:
- Automatically detect the file format
- Infer the schema and fields
- Start a FastAPI server on
http://127.0.0.1:8000 - Expose the data at
/data_20130920t1245
Generate a config file first, then serve from it:
# Step 1: Generate API config
undatum api discover data-20130920T1245.jsonl --output api-config.yml
# Step 2: Serve from config
undatum api serve --config api-config.yml --host 127.0.0.1 --port 8000Once the server is running, you can access:
Get paginated list of records (response envelope):
GET http://127.0.0.1:8000/data_20130920t1245
Example response:
{
"data": [{ "...": "..." }],
"pagination": { "limit": 50, "offset": 0, "count": 50 }
}Add include_total=true to include the total matching row count.
Filter records using query parameters:
GET http://127.0.0.1:8000/data_20130920t1245?title__like=Правительственная
GET http://127.0.0.1:8000/data_20130920t1245?pubDate__ge=2014-03-01
Supported operators: eq, ne, lt, gt, le, ge, like
Control page size and offset:
GET http://127.0.0.1:8000/data_20130920t1245?limit=10&offset=20
Sort results by field:
GET http://127.0.0.1:8000/data_20130920t1245?order_by=pubDate&order_dir=desc
GET http://127.0.0.1:8000/data_20130920t1245?sort=-pubDate
Get a single record by primary key:
GET http://127.0.0.1:8000/data_20130920t1245/{guid}
Interactive API documentation (while the server is running):
http://127.0.0.1:8000/docs
Export OpenAPI schema without starting the server:
undatum api openapi --config api-config.yml --output openapi.jsonundatum api discover <files> \
--output api-config.yml \ # Output config file path
--format-in csv \ # Override file format detection
--config-format yaml \ # Config format: yaml or json
--default-limit 50 \ # Default pagination limit
--max-limit 1000 \ # Maximum pagination limit
--allowed-ops eq,ne,lt,gt,le,ge,like # Allowed query operatorsundatum api serve \
--config api-config.yml \ # Path to API config file
--host 127.0.0.1 \ # Host to bind (default: 127.0.0.1)
--port 8000 # Port to bind (default: 8000)undatum api run <files> \
--format-in csv \ # Override file format detection
--default-limit 50 \ # Default pagination limit
--max-limit 1000 \ # Maximum pagination limit
--allowed-ops eq,ne,lt,gt,le,ge,like \ # Allowed query operators
--host 127.0.0.1 \ # Host to bind
--port 8000 # Port to bindSee api-config-example.yml for a complete example of the API configuration format.
- CSV (
.csv) - JSON Lines (
.jsonl) - Parquet (
.parquet)
You can also use the recipe system:
undatum examples run api-serve-data \
--var input=data-20130920T1245.jsonl \
--var host=127.0.0.1 \
--var port=8000Once the server is running, you can test it with curl:
# Get first 10 records
curl "http://127.0.0.1:8000/data_20130920t1245?limit=10"
# Filter by title
curl "http://127.0.0.1:8000/data_20130920t1245?title__like=Правительственная"
# Sort by date
curl "http://127.0.0.1:8000/data_20130920t1245?order_by=pubDate&order_dir=desc&limit=5"Or open the interactive docs in your browser:
http://127.0.0.1:8000/docs