# fetch

The `fetch()` function sends an HTTP request from your Azion Runtime code and returns a promise that resolves to a [Response](/en/documentation/devtools/runtime/api-reference/response/). You read that response, change it, or pass it back to the client through `event.respondWith()` or the return value of your handler. `fetch()` belongs to the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API), which also defines the [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) and [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) objects, CORS, and the semantics of the HTTP `Origin` header. With these objects, your code handles, modifies, or builds requests and responses while it answers a request.

Use `fetch()` to forward a request to an origin server and return its response, or to call a REST or GraphQL API that supplies part of a response. You can also combine data from several backends in one function, or retrieve remote assets or configuration and rewrite them before delivery.

> **Note**
>
> Under `azion dev`, a response from `fetch()` reports `type` as `default`, and `redirected` stays `false` after a followed redirect. A redirect under `redirect: 'error'` rejects with `TypeError: fetch failed`, and an expired timeout rejects with the message `The operation was aborted due to timeout`.

---

## Syntax

```javascript
fetch(request, init)
```

| Parameter | Type                   | Description                                                                   |
| --------- | ---------------------- | ----------------------------------------------------------------------------- |
| `request` | `Request` or string    | Request to send: a `Request` object, or a string that holds the URL to fetch. |
| `init`    | Object (`RequestInit`) | Options of the request.                                                       |

The `init` object takes these options. `method`, `headers`, `body`, and `redirect` are the fields that the [Request](/en/documentation/devtools/runtime/api-reference/request/#properties) object documents, and `signal` cancels the request:

| Option     | Type             | Description                                                                                                                                                                                                                                                                       |
| ---------- | ---------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `method`   | String           | Request method, such as `POST`.                                                                                                                                                                                                                                                   |
| `headers`  | `Headers` object | Headers of the request.                                                                                                                                                                                                                                                           |
| `body`     | —                | Payload of the request, such as a JSON string.                                                                                                                                                                                                                                    |
| `redirect` | String           | Redirect mode: `follow`, `error`, or `manual`. With `follow`, the default, `fetch()` follows the redirect and resolves to the final response. With `manual`, it resolves to the redirect response, such as a `301` with its `location` header. With `error`, the promise rejects. |
| `signal`   | `AbortSignal`    | Signal that cancels the request, such as `AbortSignal.timeout(300)` to stop waiting after 300 ms.                                                                                                                                                                                 |

---

## Limits

Each `fetch()` call runs inside the wall-clock ceiling of its invocation: 5 minutes, I/O wait included. One invocation can also make a limited number of outbound `fetch()` calls. For both values, refer to [Functions limits](/en/documentation/platform/functions/limits/).

To bound a single call, pass `AbortSignal.timeout()` as `signal` in `init`. The promise then rejects with an error whose `name` is `TimeoutError`, which your code catches.

---

## Errors

The promise that `fetch()` returns rejects with these errors in a deployed function:

| Error                                                                                 | Cause                                                                                                                                             | What to do                                                            |
| ------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------- |
| `TimeoutError: Signal timed out.`                                                     | The `AbortSignal.timeout()` passed as `signal` expired before the response arrived. The error is a `DOMException` whose `name` is `TimeoutError`. | Catch the rejection and check `error.name`, or pass a longer timeout. |
| `TypeError: Fetch failed: Encountered redirect while redirect mode is set to 'error'` | The server answered with a redirect, and `redirect` is `error`.                                                                                   | Set `redirect` to `follow` or `manual`, or catch the rejection.       |

---

## Example

This listener answers every request with the response that `fetch()` receives from `https://example.com`:

```javascript
addEventListener("fetch", event => {
  return event.respondWith(
    fetch("https://example.com")
  )
})
```

The listener returns a response with this status, this content type, and this body. The other response headers are cut, and the body is cut after its title:

```json
{
 "status": 200,
 "statusText": "OK",
 "headers": {
  …
  "content-type": "text/html; charset=utf-8",
  …
 },
 "body": "<!doctype html><html lang=en><head><meta charset=utf-8><link rel=icon href=data:,><meta name=viewport content=\"width=device-width,initial-scale=1\"><title>Example Domain</title>…"
}
```

---

## Related resources

- [Response](/en/documentation/devtools/runtime/api-reference/response.md): The properties and methods of the `Response` that `fetch()` resolves to.
- [Request](/en/documentation/devtools/runtime/api-reference/request.md): How to build a `Request` object to pass to `fetch()`.
- [Handlers](/en/documentation/devtools/runtime/api-reference/handlers.md): How a function receives a request and returns a response.
- [fetch on MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/API/fetch): The complete `fetch()` specification, with every parameter, the return value, and the exceptions.
