# Extract a cookie value

Read the value of a specific cookie by name directly on Azion's global network and use it to shape the response. This is helpful for A/B testing, personalization, or any logic that depends on data your application already stores in the visitor's cookies.

```js
const COOKIE_NAME = "hubspotutk"

function getCookie(request, name) {
  let result = ""
  const cookieString = request.headers.get("Cookie")
  if (cookieString) {
    const cookies = cookieString.split(";")
    cookies.forEach(cookie => {
      const cookiePair = cookie.split("=", 2)
      const cookieName = cookiePair[0].trim()
      if (cookieName === name) {
        const cookieVal = cookiePair[1]
        result = cookieVal
      }
    })
  }
  return result
}

function handleRequest(request) {
  const cookie = getCookie(request, COOKIE_NAME)
  if (cookie) {
    return new Response(cookie)
  }
  return new Response("No cookie with name: " + COOKIE_NAME)
}

addEventListener("fetch", event => {
  event.respondWith(handleRequest(event.request))
})
```

## How it works

The helper `getCookie` reads the `Cookie` header with `request.headers.get("Cookie")`, splits it on `;` into individual pairs, and splits each pair on `=` to compare the cookie name against the one you want. When a match is found, it returns that cookie's value. The `fetch` handler then calls `getCookie` and uses `event.respondWith()` with a new `Response`, returning either the cookie value or a message noting the cookie was not present.

## Related resources

- [JavaScript examples](/en/documentation/platform/functions/javascript-examples.md): Browse the other ready-to-use examples you can copy into a function.
- [FetchEvent](/en/documentation/devtools/runtime/api-reference/fetch-event.md): Reference for the `fetch` event and the request the handler receives.
- [Request](/en/documentation/devtools/runtime/api-reference/request.md): Reference for the request object whose `Cookie` header this example reads.
- [Quickstart](/en/documentation/platform/functions/quickstart.md): What an account needs before a function can run, and the steps that put one in the path of a request.
- [Query usage data from Functions](/en/documentation/guides/platform/observability/query-functions-usage-data-with-graphql.md): Read back the compute time and the invocations this function has consumed.
