# Custom Response Formatting

By default, Laminar workflows return the output of the last step with a 200 status code (if successful) or 500 (if failed). The `lam.response` feature gives you more flexibility to return custom HTTP responses with specific status codes and formatting.

This is useful when building API endpoints that need specific HTTP behaviors.

### How It Works

When any step returns an object containing `lam.response`, Laminar will:

1. **Exit immediately** - Stop executing the workflow at that step
2. **Skip remaining steps** - No subsequent steps will be executed
3. **Return custom response** - Use your specified format and status code

### Response Structure

* **statusCode** (required): HTTP status code (200, 201, 400, 404, 500, etc.)
* **message** (required): Human-readable message
* **data** (optional): Response data
* **error** (optional): Error information for error responses

{% code title="Schema" %}

```json
{
  "lam.response": {
    "statusCode": 200 | 201 | 400 | 404 | ...,
    "message": "message",
    "data": {
      // Your response data here
    },
    "error": {
      "code": "ERROR_CODE",
      "message": "Error description",
      "details": "Additional error details",
      "field_errors": {
        "field1": "Field-specific error message"
      }
    }
  }
}
```

{% endcode %}

### Examples

#### Success Response

```javascript
(data) => {
  return {
    "lam.response": {
      statusCode: 201,
      message: "Dispatch note created successfully",
      data: data.step_4.response,
    },
  };
};
```

#### Error Response

```javascript
(data) => {
  if (!data.input.email) {
    return {
      "lam.response": {
        statusCode: 400,
        message: "Email is required",
        error: {
          code: "VALIDATION_ERROR",
          field: "email",
        },
      },
    };
  }

  // Continue with normal processing
  return { validated: true };
};
```

#### Authentication Check

```javascript
(data) => {
  if (!data.input.apiKey || data.input.apiKey !== "{{config.validApiKey}}") {
    return {
      "lam.response": {
        statusCode: 401,
        message: "Invalid API key",
        error: {
          code: "UNAUTHORIZED",
        },
      },
    };
  }

  // Continue with normal workflow
  return { authorized: true };
};
```

### Use Cases

* **API endpoints** that need specific HTTP status codes
* **Early validation** to exit workflows when conditions aren't met
* **Authentication checks** with proper 401/403 responses
* **Error handling** with custom error formats


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.laminar.run/advanced/custom-response-formatting.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
