Skip to content

API response resource reference ​

This page lists every field and option. The overview shows what it does and a first example.

apiResponse: builds and returns the HTTP response sent back to the caller. It is always the last resource in the dependency chain - the resource pointed to by targetActionId in workflow.yaml.

Where it runs ​

The workflow only. apiResponse: is the terminal node that formats the HTTP response returned by a workflow. The resource is not a tool. The whole workflow is.

Basic usage ​

yaml
# resources/respond.yaml
actionId: responseResource
name: API Response
flow:
  requires:
    - llmResource
apiResponse:
  success: true
  response:
    data: get('llmResource')
  headers:
    Content-Type: application/json

Configuration options ​

yaml
# resources/example.yaml
apiResponse:
  success: true                # Boolean: request success status
  response:                    # Response body (any structure)
    field: value
    nested:
      key: value
  headers:
    Header-Name: value
  statusCode: 200              # HTTP status code (optional)
  meta:                        # optional: model/backend reported in _meta
    model: llama3.2:1b
    backend: file

Response structure ​

Simple response ​

yaml
# resources/example.yaml
apiResponse:
  success: true
  response:
    message: "Hello, World!"

Output:

json
{
  "success": true,
  "data": {
    "message": "Hello, World!"
  }
}

Dynamic response ​

yaml
# resources/example.yaml
apiResponse:
  success: true
  response:
    query: get('q')
    answer: get('llmResource').answer
    timestamp: info('timestamp')
    request_id: info('ID')

Nested structure ​

yaml
# resources/example.yaml
apiResponse:
  success: true
  response:
    user:
      id: get('user_id')
      name: get('userResource').name
    data:
      items: get('dataResource')
      count: len(get('dataResource'))
    pagination:
      page: get('page', '1')
      limit: get('limit', '10')

Custom headers ​

Set response headers:

yaml
# resources/example.yaml
apiResponse:
  success: true
  response:
    data: get('result')
  headers:
    Content-Type: application/json
    X-Request-Id: info('ID')
    X-Processing-Time: "{{ get('processingTime') }}ms"
    Cache-Control: "max-age=3600"

A non-JSON Content-Type writes response as-is instead of the {success, data, meta} envelope - for a webhook that must echo plain text, or XML/HTML:

yaml
# resources/verify.yaml - echo a challenge as text/plain
apiResponse:
  success: true
  headers:
    Content-Type: text/plain; charset=utf-8
  response: "{{ request.query['hub.challenge'] }}"

Adding metadata ​

Report a model and backend in the response _meta with meta::

yaml
# resources/example.yaml
apiResponse:
  success: true
  response:
    answer: get('llmResource')
  meta:
    model: llama3.2:1b         # reported in the response _meta
    backend: file

Automatic Metadata: If the workflow uses an LLM resource, kdeps automatically adds the model and backend to the response metadata (unless you set them explicitly).

yaml
# LLM resource used earlier
actionId: llmResource
chat:
  model:
    name: llama3.2:1b
    backend: ollama
  context:
    prompt: "{{ get('q') }}"

---
# Response automatically includes model/backend
actionId: responseResource
flow:
  requires: [llmResource]
apiResponse:
  success: true
  response:
    answer: get('llmResource')
  # model and backend added automatically

Response format:

json
{
  "success": true,
  "data": {
    "answer": "The response text..."
  },
  "meta": {
    "model": "llama3.2:1b",
    "backend": "file"
  }
}

Manual metadata ​

You can also manually specify metadata to override automatic values:

yaml
# resources/example.yaml
apiResponse:
  success: true
  response:
    answer: get('llmResource')
  model: custom-model
  backend: custom-backend
  headers:
    X-Custom-Header: value

Note: Manual metadata takes precedence over automatic metadata.

Error responses ​

For error handling, use preflight checks or conditional responses:

Preflight validation ​

yaml
# resources/example.yaml
validations:
  check:
    - get('user_id') != ''
  error:
    code: 400
    message: User ID is required

apiResponse:
  success: true
  response:
    user: get('userResource')

Conditional success ​

yaml
# resources/example.yaml
apiResponse:
  success: get('operationResource').status == 'success'
  response:
    result: get('operationResource').data
    error: get('operationResource').error

Examples ​

Chat API response ​

yaml
# resources/chat-response.yaml
actionId: chatResponse
flow:
  requires: [llmResource]
apiResponse:
  success: true
  response:
    answer: get('llmResource').answer
    model: llama3.2:1b
    usage:
      prompt_tokens: get('llmResource').prompt_tokens
      completion_tokens: get('llmResource').completion_tokens
  headers:
    Content-Type: application/json

File upload response ​

yaml
# resources/upload-response.yaml
actionId: uploadResponse
flow:
  requires: [processFile]
apiResponse:
  success: true
  response:
    message: File processed successfully
    file:
      name: get('file', 'filepath')
      type: get('file', 'filetype')
      size: get('processFile').size
    result: get('processFile').analysis
  headers:
    Content-Type: application/json

Paginated list response ​

yaml
# resources/list-response.yaml
actionId: listResponse
flow:
  requires: [fetchItems]
apiResponse:
  success: true
  response:
    items: get('fetchItems')
    pagination:
      page: get('page', '1')
      limit: get('limit', '10')
      total: get('fetchItems').total
      has_more: get('fetchItems').has_more
  headers:
    Content-Type: application/json
    X-Total-Count: get('fetchItems').total

Multi-resource response ​

yaml
# resources/dashboard-response.yaml
actionId: dashboardResponse
flow:
  requires:
    - userResource
    - statsResource
    - notificationsResource
apiResponse:
  success: true
  response:
    user:
      id: get('userResource').id
      name: get('userResource').name
      email: get('userResource').email
    stats:
      views: get('statsResource').views
      engagement: get('statsResource').engagement
    notifications:
      unread: get('notificationsResource').unread
      items: get('notificationsResource').items
  headers:
    Content-Type: application/json

Error response pattern ​

yaml
# Successful case
actionId: successResponse
flow:
  requires: [dataResource]
validations:
  skip:
  - get('dataResource').error != null

apiResponse:
  success: true
  response:
    data: get('dataResource').data

---
# Error case
actionId: errorResponse
flow:
  requires: [dataResource]
validations:
  skip:
  - get('dataResource').error == null

apiResponse:
  success: false
  response:
    error:
      code: get('dataResource').error.code
      message: get('dataResource').error.message

Response transformation ​

Transform data before you return it:

yaml
# resources/transformed-response.yaml
actionId: transformedResponse
flow:
  requires: [rawData]
after:
  - set('formatted', filter(get('rawData'), .active == true))

apiResponse:
  success: true
  response:
    data: get('formatted')
    original_count: len(get('rawData'))
    processed_count: len(get('formatted'))

Best practices ​

  1. Always set Content-Type - Usually application/json
  2. Include request ID - Helps with debugging
  3. Be consistent - Use the same structure across endpoints
  4. Include metadata - Model version, timing, and other data.
  5. Handle errors gracefully - Clear error messages

See also ​

kdeps has the Apache 2.0 License.