---
source_url: https://www.pubnub.com/docs/serverless-sdk/modules-and-libraries/xhr
title: xhr
updated_at: 2026-09-30T07:20:08.000Z
---

# xhr

## Documentation index

To discover more PubNub resources:

1. Fetch [PubNub's llms.txt](https://www.pubnub.com/llms-full.txt) for a list of available pages in Markdown format.
2. Identify relevant URLs from that index.
3. Fetch the target pages.

Do not assume a path exists, always check the index first.

The `xhr` built-in module is available only inside a PubNub Function. Its outbound HTTP and HTTPS requests count against the execution limit.

```javascript
const xhr = require('xhr');
```

## fetch

`fetch(url, http_options?)`

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| url | String | Yes |  | Target URL. |
| http_options | Object | Optional |  | Request options. See the table below. |

Returns: Promise resolving to a [response object](#response-object).

```javascript
export default (event) => {
    const xhr = require('xhr');
    return xhr.fetch('https://neutrinoapi.com/geocode-address').then((serverResponse) => {
        return event.ok();
    }).catch((err) => {
        return event.ok();
    });
};
```

```javascript
export default (event) => {
    const xhr = require('xhr');
    const http_options = {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ body: 'Posting JSON!', to: 'Someone Special!' })
    };
    return xhr.fetch('http://httpbin.org/post', http_options)
        .then((resp) => {
            const body = JSON.parse(resp.body);
            console.log(body);
            return event.ok('Request succeeded');
        })
        .catch((err) => {
            return event.ok(`Request failed: ${err}`);
        });
};
```

A POST request with a JSON body. Set both `headers` and `body` for JSON payloads.

## http_options

| Name | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `method` | String | No | `GET` | Request method. Supported: `GET`, `POST`, `PUT`, `OPTIONS`, `DELETE`, `PATCH`. |
| `body` | String | Yes for `POST` and `PUT` | None | Request payload. |
| `encoding` | String | No | `utf8` | Encoding used to decode the response body to a string. |
| `headers` | Object | No | None | Request headers. |
| `timeout` | Number | No | `5000` | Timeout in milliseconds. Maximum is `10000` (10 seconds). |
| `retries` | Number | No | `2` | Retry count. The module uses the smaller of your input and `2`. Retries on `ECONNRESET`, `ECONNREFUSED`, and `EMFILE`. |

## Response object

| Name | Type | Description |
| --- | --- | --- |
| `body` | String | Decoded response body, using the default or specified `encoding`. |
| `buffer` | Object | Response body buffer. |
| `headers` | Object | Response headers. |
| `url` | String | Request URL. |
| `status` | Number | Status code. |
| `statusText` | String | Status text. |
| `ok` | Boolean | `true` if the status code is `2XX`. |
| `bodyUsed` | Boolean | Whether the body has been read. |

```javascript
export default (request) => {
    const xhr = require('xhr');
    return xhr.fetch('https://httpbin.org/status/200', {
        headers: { 'Content-Type': 'application/json' }
    })
    .then((res) => {
        console.log(res);
        return request.ok('Request succeeded.');
    })
    .catch((err) => {
        return request.abort(`Request failed: ${err}`);
    });
};
```

`fetch()` rejects on non-2XX responses and transport errors. Handle the rejection in `.catch()`.

## Related reference

* [Modules and libraries](https://www.pubnub.com/docs/serverless-sdk/modules-and-libraries/overview.md). Every built-in module available inside a Function.
* [API limits](https://www.pubnub.com/docs/serverless-sdk/api-limits.md). Per-execution XHR request cap.

Last updated at: 2026-09-30T07:20:08.000Z
