---
sidebar_position: 1
hide_title: false
hide_table_of_contents: false
pagination_next: null
pagination_prev: null
---


# JavaScript for Fastly Compute

This site is the full SDK reference for [`@fastly/js-compute`](https://www.npmjs.com/package/%40fastly/js-compute), the module that provides the interface between your JavaScript code and the [Fastly Compute](https://www.fastly.com) platform.

If you haven't used Fastly Compute before, start by [setting up your first Fastly Compute program](https://developer.fastly.com/learning/compute/) over on **developer.fastly.com** so that you have a working development environment.

To learn more about the fundamentals of using JavaScript with Fastly Compute, see our [using JavaScript](https://developer.fastly.com/learning/compute/javascript/) guide.

## Understanding the JavaScript SDK

Incoming HTTP requests to [domains that are attached to your Fastly service](https://developer.fastly.com/learning/concepts/) will start an instance of your application and invoke a `fetch` event, which can be bound using the `addEventListener` function:

```js
addEventListener("fetch", event => event.respondWith(handleRequest(event)) );

async function handleRequest(event) {
  const req = event.request;

  return fetch(req, {
    backend: "example_backend"
  });
}
```

Fastly specific features are available as named imports from `fastly:` prefixed modules, all of which are documented in this site.  For example, the [env](https://js-compute-reference-docs.edgecompute.app/docs/fastly:env/env) function provides access to [environment variables](https://developer.fastly.com/reference/compute/ecp-env/) and can be imported into your application like this:

```js
import { env } from "fastly:env"
```

JavaScript code compiled for Fastly Compute has access to a global environment with most of the globals you would expect in an ECMAScript runtime, like [`Date`](https://js-compute-reference-docs.edgecompute.app/docs/globals/Date/) and [`console`](https://js-compute-reference-docs.edgecompute.app/docs/globals/console/log).

## Trying things out

[Fastly fiddle](https://fiddle.fastly.dev) is an online web-based playground where you can run Fastly code.  You'll see fiddles included in many pages of our [developer hub](https://developer.fastly.com) and this SDK reference. These interactive examples can be executed right on the page by clicking the **RUN** tab:

<Fiddle config={{
  "type": "javascript",
  "title": "Example",
  "origins": [
    "https://http-me.fastly.dev"
  ],
  "src": {
    "deps": "{\n  \"@fastly/js-compute\": \"^1.0.1\"\n}",
    "main": `
/// <reference types="@fastly/js-compute" />
async function app(event) {
    const request = event.request;
    return new Response("You made a request to " + request.url)
}
addEventListener("fetch", event => {
  event.respondWith(app(event));
});
`
  },
  "requests": [
    {
      "enableCluster": true,
      "enableShield": false,
      "enableWAF": false,
      "method": "GET",
      "path": "/status=200",
      "useFreshCache": false,
      "followRedirects": false,
      "tests": "",
      "delay": 0
    }
  ],
  "srcVersion": 1
}}>

```js
/// <reference types="@fastly/js-compute" />

async function app(event) {
    const request = event.request;
    return new Response(`You made a request to ${request.url}`)
}

addEventListener("fetch", event => {
  event.respondWith(app(event));
});

```

</Fiddle>

Check out [`fiddle.fastly.dev`](https://fiddle.fastly.dev) to create your own.
