> For the complete documentation index, see [llms.txt](/llms.txt).

# Network access

You can access the internet from a Snap using the global `fetch` API.

## Steps[​](#steps "Direct link to Steps")

### 1. Request permission to access the internet[​](#1-request-permission-to-access-the-internet "Direct link to 1. Request permission to access the internet")

Request the [endowment:network-access](/snaps/reference/permissions/#endowmentnetwork-access) permission, which exposes the global `fetch` API to the Snaps execution environment. Add the following to your Snap's manifest file:

snap.manifest.json

```
"initialPermissions": {
  "endowment:network-access": {}
}

```

### 2. Use the `fetch` function[​](#2-use-the-fetch-function "Direct link to 2-use-the-fetch-function")

You can now use the `fetch` function to access the internet. The following example fetches a JSON file from the provided URL.

index.ts

```
async function getJson(url: string) {
  const response = await fetch(url)
  return await response.json()
}

```

Same-origin policy and CORS

`fetch` requests in a Snap are bound by the browser's [same-origin policy](https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin%5Fpolicy#cross-origin%5Fnetwork%5Faccess). Since Snap code is executed in an iframe with the `sandbox` property, the browser sends an `Origin`header with the value `null` with outgoing requests. For the Snap to be able to read the response, the server must send an [Access-Control-Allow-Origin](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) CORS header with the value `*` or `null` in the response. Otherwise, you might need to [set up a proxy](https://stackoverflow.com/questions/43871637/no-access-control-allow-origin-header-is-present-on-the-requested-resource-whe/43881141#43881141).

caution

`XMLHttpRequest` isn't available in Snaps, and you should replace it with `fetch`. If your dependencies use `XMLHttpRequest`, you can [patch it away](/snaps/how-to/debug-a-snap/common-issues/#patch-the-use-of-xmlhttprequest).

## Example[​](#example "Direct link to Example")

See the [@metamask/network-access-example-snap](https://github.com/MetaMask/snaps/tree/main/packages/examples/packages/network-access)package for a full example of accessing the internet from a Snap. This example exposes a [custom JSON-RPC API](/snaps/learn/about-snaps/apis/#custom-json-rpc-apis) for dapps to call the `fetch` function from a Snap.
