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

# Create Saved Query

> Create a new saved query

<ParamField header="Authorization" type="string" required>
  Bearer token obtained from the authenticate endpoint.
</ParamField>

<ParamField body="title" type="string" required>
  Display title for the query.
</ParamField>

<ParamField body="prompt" type="string" required>
  The natural language instruction that describes what this query does. Passed to the AI agent when the query is invoked via chat.
</ParamField>

<ParamField body="sql" type="string">
  The SQL to execute when this query is invoked. Use `{{param_name}}` placeholders for values that should be supplied at runtime (e.g. `WHERE year = {{year}}`). The server automatically extracts these into the `parameters` field on the response. If omitted, the query is prompt-only — the `prompt` is passed directly to the AI agent without pre-executing any SQL.
</ParamField>

<ParamField body="description" type="string">
  Optional human-readable description of what the query does.
</ParamField>

<ParamField body="keywords" type="string[]">
  Optional list of keywords for automatic semantic matching. When a chat message's content matches these terms, this query may be surfaced automatically.
</ParamField>

## Response

Returns the created saved query object.

<ResponseField name="id" type="string" required>
  Unique identifier for the newly created query.
</ResponseField>

<ResponseField name="organization_id" type="string" required>
  Organization the query belongs to.
</ResponseField>

<ResponseField name="title" type="string" required>
  Display title for the query.
</ResponseField>

<ResponseField name="description" type="string">
  Description of the query, if provided.
</ResponseField>

<ResponseField name="prompt" type="string" required>
  The natural language instruction for this query.
</ResponseField>

<ResponseField name="sql" type="string">
  The SQL associated with this query, or `null` for prompt-only queries.
</ResponseField>

<ResponseField name="keywords" type="string[]">
  Keywords for semantic matching, if provided.
</ResponseField>

<ResponseField name="parameters" type="string[]">
  Names of all `{{param_name}}` placeholders detected in `sql`, auto-extracted by the server. This field is read-only.
</ResponseField>

<ResponseField name="last_updated_by" type="string" required>
  Member ID of the user who created this query.
</ResponseField>

<ResponseField name="last_updated_by_name" type="string">
  Display name of the user who created this query.
</ResponseField>

<ResponseField name="created_at" type="string" required>
  ISO 8601 timestamp when the query was created.
</ResponseField>

<ResponseField name="updated_at" type="string" required>
  ISO 8601 timestamp when the query was created.
</ResponseField>

<RequestExample>
  ```bash cURL theme={"theme":{"light":"github-light","dark":"github-dark"}}
  curl -X POST https://api.trellis.sh/v1/queries \
    -H "Authorization: Bearer YOUR_TOKEN" \
    -H "Content-Type: application/json" \
    -d '{
      "title": "Top Customers",
      "prompt": "Who are our top customers by total spend?",
      "sql": "SELECT customer_id, SUM(amount) AS total_spend FROM orders GROUP BY customer_id ORDER BY total_spend DESC LIMIT {{limit}}",
      "description": "Returns the top N customers ranked by total spend",
      "keywords": ["customers", "spend", "top"]
    }'
  ```

  ```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
  import requests

  response = requests.post(
      "https://api.trellis.sh/v1/queries",
      headers={
          "Authorization": "Bearer YOUR_TOKEN",
          "Content-Type": "application/json"
      },
      json={
          "title": "Top Customers",
          "prompt": "Who are our top customers by total spend?",
          "sql": "SELECT customer_id, SUM(amount) AS total_spend FROM orders GROUP BY customer_id ORDER BY total_spend DESC LIMIT {{limit}}",
          "description": "Returns the top N customers ranked by total spend",
          "keywords": ["customers", "spend", "top"]
      }
  )
  new_query = response.json()
  ```

  ```javascript JavaScript theme={"theme":{"light":"github-light","dark":"github-dark"}}
  const response = await fetch("https://api.trellis.sh/v1/queries", {
    method: "POST",
    headers: {
      Authorization: "Bearer YOUR_TOKEN",
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      title: "Top Customers",
      prompt: "Who are our top customers by total spend?",
      sql: "SELECT customer_id, SUM(amount) AS total_spend FROM orders GROUP BY customer_id ORDER BY total_spend DESC LIMIT {{limit}}",
      description: "Returns the top N customers ranked by total spend",
      keywords: ["customers", "spend", "top"],
    }),
  });
  const newQuery = await response.json();
  ```
</RequestExample>

<ResponseExample>
  ```json 200 theme={"theme":{"light":"github-light","dark":"github-dark"}}
  {
    "id": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
    "organization_id": "org_abc123",
    "title": "Top Customers",
    "description": "Returns the top N customers ranked by total spend",
    "prompt": "Who are our top customers by total spend?",
    "sql": "SELECT customer_id, SUM(amount) AS total_spend FROM orders GROUP BY customer_id ORDER BY total_spend DESC LIMIT {{limit}}",
    "keywords": ["customers", "spend", "top"],
    "parameters": ["limit"],
    "last_updated_by": "member_xyz789",
    "last_updated_by_name": "Jane Smith",
    "created_at": "2025-01-11T10:00:00Z",
    "updated_at": "2025-01-11T10:00:00Z"
  }
  ```

  ```json 400 theme={"theme":{"light":"github-light","dark":"github-dark"}}
  {
    "detail": "Title is required"
  }
  ```
</ResponseExample>
