Skip to main content

Request context

The createShopifyRequestContext function creates the context for one incoming request. Use the context to forward the customer's cookies, locale, and IP address to Shopify, and to apply Hydrogen's response headers.

Import createShopifyRequestContext from @shopify/hydrogen.

Create one context per request, and pass the same context to createStorefrontClient, createCustomerAccountClient, and handleShopifyRoutes.


Creates the context for one incoming request. Pass the same context to the Storefront API client, the Customer Account API client, and handleShopifyRoutes. The context forwards the customer's cookies, locale, and privacy signal to Shopify, and adds Shopify's cookies and cache headers to your response.

Call applyResponseHeaders() on each response that your app builds. Pass buyerIp with the customer's trusted IP address when you use a private Storefront API client. The function throws when i18n lacks a country or language, and when buyerIp is an empty string.

Anchor to input
input
<I18n> & { buyerIp?: string; }
required

The incoming request, its locale, and the customer's IP address for private Storefront API clients.

<const I18n extends >

The context to pass to Hydrogen's API clients and route handlers for this request.

Examples

TypeScript

import {
createShopifyRequestContext,
createStorefrontClient,
type StorefrontClient,
} from "@shopify/hydrogen";

export async function handleRequest(
request: Request,
next: (storefront: StorefrontClient) => Promise<Response>,
) {
const requestContext = createShopifyRequestContext({
request,
i18n: { country: "US", language: "EN" },
});

const storefront = createStorefrontClient({
type: "public",
requestContext,
config: { storeDomain: process.env.PUBLIC_STORE_DOMAIN! },
});

const routedResponse = await next(storefront);
const response = new Response(routedResponse.body, routedResponse);
requestContext.applyResponseHeaders(response.headers);
return response;
}

Was this page helpful?