env

Functionin env@fastly/js-compute 4.0.0View source
env(name: string): string | undefined

Function to get the value for the provided environment variable name.

For a list of available environment variables, see the Fastly Developer Hub for Compute Environment Variables

Note: The environment variables can only be retrieved when processing requests, not during build-time initialization.

Parameters

name: string

The name of the environment variable

Returns

string | undefined

The value of the environment variable, or undefined if no such environment variable exists.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<script async defer src="https://fiddle.fastly.dev/embed.js"></script>
In this example we log to stdout the environment variables `FASTLY_HOSTNAME` and `FASTLY_TRACE_ID`.
<script type="application/json+fiddle">
{
"type": "javascript",
"title": "Environment Variable Example",
"origins": [
"https://http-me.fastly.dev"
],
"src": {
"deps": "{\n \"@fastly/js-compute\": \"^0.7.0\"\n}",
"main": "/// <reference types=\"@fastly/js-compute\" />\nimport { env } from \"fastly:env\";\n\nfunction app(event) {\n console.log(\"FASTLY_HOSTNAME:\", env(\"FASTLY_HOSTNAME\"));\n console.log(\"FASTLY_TRACE_ID:\", env(\"FASTLY_TRACE_ID\"));\n\n return new Response(\"\", {\n status: 200\n });\n}\n\naddEventListener(\"fetch\", event => event.respondWith(app(event)));\n"
},
"requests": [
{
"enableCluster": true,
"enableShield": false,
"enableWAF": false,
"method": "GET",
"path": "/status=200",
"useFreshCache": false,
"followRedirects": false,
"tests": "",
"delay": 0
}
],
"srcVersion": 26
}
</script>
<noscript>
```js
/// <reference types="@fastly/js-compute" />
import { env } from "fastly:env";
function app(event) {
console.log("FASTLY_HOSTNAME:", env("FASTLY_HOSTNAME"));
console.log("FASTLY_TRACE_ID:", env("FASTLY_TRACE_ID"));
return new Response("", {
status: 200
});
}
addEventListener("fetch", event => event.respondWith(app(event)));
```
</noscript>