# Retrying HTTP Requests

When working with external APIs, network failures and temporary service outages are inevitable. The `retry` option in `lam.httpRequest` and `lam.httpRequests` allows you to automatically retry failed requests.

The retry functionality is **optional;** just add a `retry` object with `maxAttempts` to your HTTP request.

**Important:** Retries are not enabled by default. You must explicitly specify retry behaviour when creating your workflow steps.<br>

**`retry` Properties**

<table><thead><tr><th width="200.11328125">Property</th><th width="148.4609375">Type</th><th width="206.83203125">Default</th><th>Description</th></tr></thead><tbody><tr><td>maxAttempts</td><td>int</td><td>required</td><td>Total number of attempts (1 = no<br>retry)</td></tr><tr><td>strategy</td><td>string</td><td>exponential</td><td>"exponential" or "fixed"</td></tr><tr><td>initialDelay</td><td>string/number</td><td>1s</td><td>First retry delay</td></tr><tr><td>maxDelay</td><td>string/number</td><td>30s</td><td>Maximum delay cap</td></tr><tr><td>multiplier</td><td>double</td><td>2.0</td><td>Exponential backoff multiplier</td></tr><tr><td>retryableStatusCodes</td><td>array[int]</td><td>[429,500,502,503,504]</td><td>HTTP status codes to retry</td></tr><tr><td>retryableExceptions</td><td>array[string]</td><td>["timeout","connection"]</td><td>Exception types to retry</td></tr></tbody></table>

**Example:** Suppose we're processing orders and need to reliably send them to external fulfillment services that occasionally experience temporary outages.

{% stepper %}
{% step %}

#### Send Order to Primary Warehouse (With Retry)

```javascript
(data) => {
  const { input } = data;

  return {
    "lam.httpRequest": {
      method: "POST",
      url: "{{config.primaryWarehouseUrl}}/api/orders",
      headers: {
        Authorization: "Bearer {{config.warehouseApiKey}}",
        "Content-Type": "application/json",
      },
      body: {
        orderId: input.orderId,
        items: input.items,
        priority: "high",
        shippingAddress: input.shippingAddress,
      },
      // added here
      retry: {
        maxAttempts: 3,
      },
    },
  };
};
```

{% endstep %}

{% step %}

#### Update Multiple Services (Batch Retry)

```javascript
(data) => {
  const { input } = data;
  const fulfillmentResponse = data.step_2.response || data.step_1.response;

  return {
    "lam.httpRequests": [
      {
        method: "PUT",
        url: `{{config.orderServiceUrl}}/orders/${input.orderId}/status`,
        headers: {
          Authorization: "Bearer {{config.orderServiceKey}}",
        },
        body: {
          status: "processing",
          trackingNumber: fulfillmentResponse.trackingNumber,
          warehouse: fulfillmentResponse.warehouseId,
        },
        // added here
        retry: {
          maxAttempts: 2,
        },
      },
      {
        method: "POST",
        url: "{{config.notificationServiceUrl}}/send-confirmation",
        headers: {
          Authorization: "Bearer {{config.notificationKey}}",
        },
        body: {
          customerId: input.customerId,
          orderId: input.orderId,
          trackingNumber: fulfillmentResponse.trackingNumber,
          type: "order_confirmed",
        },
        // added here
        retry: {
          maxAttempts: 4,
        },
      },
    ],
  };
};
```

{% endstep %}
{% endstepper %}


---

# 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/retrying-http-requests.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.
