> For the complete documentation index, see [llms.txt](https://docs.laminar.run/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.laminar.run/advanced/custom-response-formatting.md).

# 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
