FiveonefourFiveonefour
Fiveonefour Docs
MooseStackTemplatesGuides
Release Notes
Source514
  1. MooseStack
  2. Moose APIs & Web Apps
  3. APIs

On this page

OverviewEnabling APIsBasic UsageType ValidationModeling Query ParametersAdding Advanced Type ValidationCommon Validation OptionsSetting Default ValuesImplementing Route HandlerConnecting to the DatabaseCreate the APIConstructing Safe SQL QueriesAdvanced Query PatternsAdding AuthenticationUnderstanding Response CodesPost-Processing Query ResultsBest PracticesClient Integration

APIs

Overview

APIs are functions that run on your server and automatically exposed as HTTP GET endpoints. They are designed to read data from your OLAP database. Out of the box, these APIs provide:

  • Automatic type validation and type conversion for your query parameters, which are sent in the URL, and response body
  • Managed database client connection
  • Automatic OpenAPI documentation generation

Common use cases include:

  • Powering user-facing analytics, dashboards and other front-end components
  • Enabling AI tools to interact with your data
  • Building custom APIs for your internal tools

Enabling APIs

Analytics APIs are enabled by default. To explicitly control this feature in your moose.config.toml:

moose.config.toml
[features]apis = true

Basic Usage

ExampleApi.ts
import { Api } from "@514labs/moose-lib";import { SourcePipeline } from "path/to/SourcePipeline";  // Define the query parametersinterface QueryParams {  filterField: string;  maxResults: number;} // Model the query result typeinterface ResultItem {  id: number;  name: string;  value: number;}  const SourceTable = SourcePipeline.table!; // Use `!` to assert that the table is not nullconst cols = SourceTable.columns; // Define the result type as an array of the result item typeexport const exampleApi = new Api<QueryParams, ResultItem[]>("example_endpoint",      async ({ filterField, maxResults }: QueryParams, { client, sql }) => {        const query = sql`        SELECT           ${cols.id},          ${cols.name},          ${cols.value}        FROM ${SourceTable}        WHERE category = ${filterField}        LIMIT ${maxResults}`;                // Set the result type to the type of the each row in the result set        const resultSet = await client.query.execute<ResultItem>(query);         // Return the result set as an array of the result item type        return await resultSet.json();    });

The Api class takes:

  • Route name: The URL path to access your API (e.g., "example_endpoint")
  • Handler function: Processes requests with typed parameters and returns the result

The generic type parameters specify:

  • QueryParams: The structure of accepted URL parameters
  • ResponseBody: The exact shape of your API's response data
MooseTip:

You can name these types anything you want. The first type generates validation for query parameters, while the second defines the response structure for OpenAPI documentation.

Moose automatically handles:

URL parameter validation and type conversion

SQL query interpolation and execution

Response formatting

Automated OpenAPI documentation

Type Validation

You can also model the query parameters and response body as interfaces (TypeScript) or Pydantic models (Python), which Moose will use to provide automatic type validation and type conversion for your query parameters, which are sent in the URL, and response body.

Modeling Query Parameters

Define your API's parameters as a Pydantic model:

ExampleQueryParams.py
from pydantic import BaseModelfrom typing import Optional class QueryParams(BaseModel):    filterField: str = Field(..., description="The field to filter by")    maxResults: int = Field(..., description="The maximum number of results to return")    optionalParam: Optional[str] = Field(None, description="An optional parameter")

Moose automatically handles:

  • Runtime validation
  • Clear error messages for invalid parameters
  • OpenAPI documentation generation
Warning:

Complex nested objects and arrays are not supported. Analytics APIs are GET endpoints designed to be simple and lightweight.

Adding Advanced Type Validation

Moose uses Pydantic for runtime validation. Use Pydantic's Field class for more complex validation:

ExampleQueryParams.py
from pydantic import BaseModel, Field class QueryParams(BaseModel):    filterField: str = Field(pattern=r"^(id|name|email)$", description="The field to filter by") ## Only allow valid column names from the UserTable    maxResults: int = Field(gt=0, description="The maximum number of results to return") ## Positive integer

Common Validation Options

ValidationExamples.py
from pydantic import BaseModel, Field class QueryParams(BaseModel):    # Numeric validations    id: int = Field(..., gt=0)    age: int = Field(..., gt=0, lt=120)    price: float = Field(..., gt=0, lt=1000)    discount: float = Field(..., gt=0, multiple_of=0.5)        # String validations    username: str = Field(..., min_length=3, max_length=20)    email: str = Field(..., format="email")     zipCode: str = Field(..., pattern=r"^[0-9]{5}$")    uuid: str = Field(..., format="uuid")    ipAddress: str = Field(..., format="ipv4")        # Date validations    startDate: str = Field(..., format="date")        # Enum validation    status: str = Field(..., enum=["active", "pending", "inactive"])        # Optional parameters    limit: int = Field(None, gt=0, lt=100)

For a full list of validation options, see the Pydantic documentation.

Setting Default Values

You can set default values for parameters by setting values for each parameter in your Pydantic model:

ExampleQueryParams.py
from pydantic import BaseModel class QueryParams(BaseModel):    filterField: str = "example"    maxResults: int = 10    optionalParam: str | None = "default"

Implementing Route Handler

API route handlers are regular functions, so you can implement whatever arbitrary logic you want inside these functions. Most of the time you will be use APIs to expose your data to your front-end applications or other tools:

Connecting to the Database

Moose provides a managed MooseClient to your function execution context. This client provides access to the database and other Moose resources, and handles connection pooling/lifecycle management for you:

Constructing Safe SQL Queries

Basic Query Parameter Interpolation

ValidatedQueries.py
from moose_lib import Api, MooseClientfrom pydantic import BaseModel, Field, constrfrom typing import Literal, Optionalfrom enum import Enumfrom app.UserTable import UserTable class QueryParams(BaseModel):    # When using f-strings, we need extremely strict validation    column: str = Field(pattern=r"^(id|name|email)$", description="Uses a regex pattern to only allow valid column names")        search_term: str = Field(        pattern=r'^[\w\s\'-]{1,50}$',  # Allows letters, numbers, spaces, hyphens, apostrophes; Does not allow special characters that could be used in SQL injection        strip_whitespace=True,        min_length=1,        max_length=50    )        limit: int = Field(        default=10,        ge=1,        le=100,        description="Number of results to return"    ) def run(client: MooseClient, params: QueryParams):    query = """        SELECT {column}        FROM {table}        WHERE name ILIKE '%{search_term}%'        LIMIT {limit}    """        return client.query.execute(query, {"column": UserTable.cols[params.column], "table": UserTable, "search_term": params.search_term, "limit": params.limit})

Table and Column References

Advanced Query Patterns

Dynamic Column & Table Selection

DynamicColumns.py
from app.UserTable import UserTable class QueryParams(BaseModel):    colName: str = Field(pattern=r"^(id|name|email)$", description="Uses a regex pattern to only allow valid column names from the UserTable") class QueryResult(BaseModel):    id: Optional[int]    name: Optional[str]    email: Optional[str] def run(client: MooseClient, params: QueryParams):    # Put column and table in the dict for variables    query = "SELECT {column} FROM {table}"    return client.query.execute(query, {"column": UserTable.cols[params.colName], "table": UserTable}) # Create the APIbar = Api[QueryParams, QueryResult](name="bar", query_function=run) # Call the API# HTTP Request: GET http://localhost:4000/api/bar?colName=id# EXECUTED QUERY: SELECT id FROM users

Conditional WHERE Clauses

Build WHERE clauses based on provided parameters:

ConditionalColumns.py
class FilterParams(BaseModel):    min_age: Optional[int]    status: Optional[str] = Field(pattern=r"^(active|inactive)$")    search_text: Optional[str] = Field(pattern=r"^[a-zA-Z0-9\s]+$", description="Alphanumeric search text without special characters to prevent SQL injection") class QueryResult(BaseModel):    id: int    name: str    email: str def build_query(client: MooseClient, params: FilterParams) -> QueryResult:    # Using f-strings with validated parameters    conditions = []    parameters = {}    if params.min_age:        conditions.append("age >= {min_age}")        parameters["min_age"] = params.min_age        if params.status:        conditions.append("status = {status}")        parameters["status"] = params.status        if params.search_text:        conditions.append("(name ILIKE {search_text} OR email ILIKE {search_text})")        parameters["search_text"] = params.search_text     where_clause = f" WHERE {' AND '.join(conditions)}" if conditions else ""    query = f"""SELECT * FROM users {where_clause} ORDER BY created_at DESC"""    return client.query.execute(query, parameters) # Create the APIbar = Api[FilterParams, QueryResult](name="bar", query_function=build_query) # Call the API# HTTP Request: GET http://localhost:4000/api/bar?min_age=20&status=active&search_text=John# EXECUTED QUERY: SELECT * FROM users WHERE age >= 20 AND status = 'active' AND (name ILIKE '%John%' OR email ILIKE '%John%') ORDER BY created_at DESC

Adding Authentication

Moose supports authentication via JSON web tokens (JWTs). When your client makes a request to your Analytics API, Moose will automatically parse the JWT and pass the authenticated payload to your handler function as the jwt object:

Authentication.py
def run(client: MooseClient, params: QueryParams, jwt: dict):    # Use parameter binding with JWT data    query = """SELECT * FROM userReports WHERE user_id = {user_id} LIMIT 5"""    return client.query.execute(query, {"user_id": jwt["userId"]})
JWT Error Handling

Moose validates the JWT signature and ensures the JWT is properly formatted. If the JWT authentication fails, Moose will return a 401 Unauthorized error.

Understanding Response Codes

Moose automatically provides standard HTTP responses:

Status CodeMeaningResponse Body
200SuccessYour API's result data
400Validation error{ "error": "Detailed message"}
401Unauthorized{ "error": "Unauthorized"}
500Internal server error{ "error": "Internal server error"}

Post-Processing Query Results

After executing your database query, you can transform the data before returning it to the client. This allows you to:

Common post-processing operations:

Transform field names or data formats

Calculate derived values

Filter or sort results

Aggregate or group data

Apply business logic

PostProcessingExample.py
from datetime import datetimefrom moose_lib import Apifrom pydantic import BaseModel class QueryParams(BaseModel):    category: str    max_results: int = 10 class ResponseItem(BaseModel):    itemId: int    displayName: str    formattedValue: str    isHighValue: bool    date: str def run(client: MooseClient, params: QueryParams):    # 1. Fetch raw data using parameter binding    query = """    SELECT id, name, value, timestamp    FROM data_table    WHERE category = {category}    LIMIT {limit}    """        raw_results = client.query.execute(query, {"category": params.category, "limit": params.max_results})        # 2. Post-process the results    processed_results = []    for row in raw_results:        processed_results.append(ResponseItem(            # Transform field names            itemId=row['id'],            displayName=row['name'].upper(),                        # Add derived fields            formattedValue=f"${row['value']:.2f}",            isHighValue=row['value'] > 1000,                        # Format dates            date=datetime.fromisoformat(row['timestamp']).date().isoformat()        ))        return processed_results # Create the APIprocess_data_api = Api[QueryParams, ResponseItem](name="process_data_endpoint", query_function=run)

Best Practices

Post-Processing Best Practices

Prefer database processing for large datasets

When working with large amounts of data, perform as much filtering, grouping, and aggregation as possible in your SQL query

Keep response size reasonable

Post-process to reduce response size when needed, especially for user-facing APIs

Format dates and numbers consistently

Ensure consistent formatting for dates, currencies, and other values in your responses

Handle sensitive data appropriately

Use post-processing to remove or mask sensitive information before returning data to clients

Add clear error handling

Include appropriate error handling in your post-processing logic

MooseTip:

While post-processing gives you flexibility, remember that database operations are typically more efficient for heavy data manipulation. Reserve post-processing for transformations that are difficult to express in SQL or that involve application-specific logic.

Client Integration

By default, all API endpoints are automatically integrated with OpenAPI/Swagger documentation. You can integrate your OpenAPI SDK generator of choice to generate client libraries for your APIs.

Please refer to the OpenAPI page for more information on how to integrate your APIs with OpenAPI.

  • Overview
Build a New App
  • 5 Minute Quickstart
  • Browse Templates
  • Existing ClickHouse
Add to Existing App
  • Next.js
  • Fastify
Fundamentals
  • Moose Runtime
  • MooseDev MCP
  • Data Modeling
Moose Modules
  • Moose OLAP
  • Moose Streaming
  • Moose Workflows
  • Moose APIs & Web Apps
    • Native APIs
    • Ingest API
    • Analytics API
    • Workflow Trigger
    • Admin APIs
    • Authentication
    • Use Your Web Framework
    • Overview
    • FastAPI
Deployment & Lifecycle
  • Moose Migrate
  • Moose Deploy
Reference
  • API Reference
  • Data Types
  • Table Engines
  • CLI
  • Configuration
  • Observability Metrics
  • Help
  • Release Notes
Contribution
  • Documentation
  • Framework