Shopify routes
The handleShopifyRoutes function serves Shopify's storefront endpoints, such as checkout permalinks and the Storefront API proxy, and your custom route handlers. Use createShopifyRouteHandler to add your own endpoints that receive the request context, the session, and the Storefront API client.
Import handleShopifyRoutes and createShopifyRouteHandler from @shopify/hydrogen.
Anchor to Shopify endpointsShopify endpoints
Serves Shopify's storefront endpoints and your custom route handlers from your app. Call the function at the start of request handling, before your framework's router.
When a route matches, the function returns a promise for the response. Return that response as is, because the response already carries the request context's headers. When no route matches, the function returns null synchronously, and your framework's router handles the request. The function throws when differs from the Storefront API client's request context. When a route handler throws, the returned promise rejects. Inside a try block, use return await on the result.
The function checks these routes in order and returns the first match: the Shopify API proxy at /__shopify, the Storefront API proxy at , Liquid-style ?variant= product URLs, registered handler groups, UCP buy permalinks at , checkout and cart permalinks, allowlisted .well-known resources, the UCP MCP proxy at , the MCP proxy at , and AJAX cart URLs.
Some routes answer unsupported methods with a 405 response. A registered handler path returns 405 for any method that no handler on that path registers. Checkout and cart permalinks accept only GET and HEAD. Buy permalinks accept only GET, and the UCP MCP proxy accepts only POST.
When your bundler resolves the package's development export condition, the function also serves the GraphiQL explorer at /graphiql for GET requests that match no other route, and accepts a graphiql option. Set to an object with , , and to add a Customer Account API tab. The explorer skips the tab when it can't fetch the schema.
Anchor to handleShopifyRoutes-parametersParameters
- Anchor to optionsoptionsoptionsHydrogenRoutesOptionsHydrogenRoutesOptionsrequiredrequired
The request, its context and session, the Storefront API client, your route templates, and your handler groups.
A promise for the matched route's response, or
nullwhen no route matches.
HydrogenRoutesOptions
The request, its context and session, the Storefront API client, your route templates, and your handler groups.
- handlers
The handler groups for your app's custom endpoints.
readonly ShopifyRouteHandlerGroup[] - request
The incoming request that matched the handler's pathname and method.
Request - requestContext
The current request's context, the same one the Storefront API client uses.
ShopifyRequestContext - routeTemplates
Your app's route templates. Hydrogen redirects Liquid-style `?variant=` URLs on your product paths and on Shopify's default product paths.
ShopifyRouteTemplates - sessionManager
Your app's session storage for the current request.
ShopifyRouteSessionManager - storefrontClient
The Storefront API client for the current request.
StorefrontClient
ShopifyRouteHandlerGroup
An object of route handlers keyed by name. Pass an array of groups to `handleShopifyRoutes` as `handlers`.
ShopifyRouteHandler
ShopifyRequestContext
The context for one request. Pass the context to Hydrogen's API clients and route handlers for that request.
ShopifyRequestContextBase & {
i18n: NormalizedI18nConfig<I18n>;
}ShopifyRequestContextBase
The fields and methods on every request context.
- __hydrogenShopifyRequestContextBrand
A type-only marker. Create request contexts only with `createShopifyRequestContext`.
never - applyResponseHeaders
Adds the headers that a Hydrogen storefront needs to the final response. Call the method on each response that your app builds. Responses from `handleShopifyRoutes` and `handleShopifyRedirects` already carry these headers. Append your committed session headers before you call the method. The method makes any response that sets a cookie private and uncacheable. The method also sets the `powered-by` header, makes responses that hold customer data private and uncacheable, and adds eligible Shopify cookies only to non-document responses for methods other than `GET` and `HEAD`.
(headers: Headers) => void - applyStorefrontRequestHeaders
Adds the Hydrogen SDK headers and the customer's cookie, privacy, and origin headers to a Storefront API request. The Storefront API client and the API proxies call the method for you.
(headers: Headers) => void - buyerIp
The customer's trusted IP address. Private Storefront API clients require it.
string - captureSubrequestHeaders
Saves the cookies from the first Storefront API response that sets cookies. The Storefront API client calls the method for you, and `applyResponseHeaders()` can add the cookies to your response.
(headers: Headers) => void - consumeStorefrontResponseHeaders
Saves the cookies from a proxied Storefront API response and removes the `set-cookie` header from that response. The API proxies in `handleShopifyRoutes` call the method for you.
(headers: Headers) => void - cookie
The incoming request's `Cookie` header. Hydrogen forwards the header to the Storefront API.
string - getForwardedRequestHeaders
Returns the incoming request's headers with Shopify's request headers added. Pass the headers when you forward the request to a proxy or another origin.
() => Headers - i18n
The request's language, country, and path prefix. The path prefix starts with `/` and has no trailing slash, or is an empty string when you don't set one.
NormalizedI18nConfig - markResponseAsPersonalized
Marks the final response as private and uncacheable because the response holds customer data. The Customer Account API client and the customer session call the method for you.
(reason: string) => void - markResponseAsSessionEstablishing
Lets the final response set Shopify cookies before the customer has a Shopify session, and marks the response as private and uncacheable. The UCP MCP proxy calls the method for you.
(reason: string) => void - requestGroupId
The ID that groups this request's Shopify API calls. Hydrogen reads the ID from the request headers or generates one.
string - signal
The incoming request's abort signal. Hydrogen's API clients stop their requests when the signal aborts.
AbortSignal - storefrontOrigin
The origin of the incoming request's URL. Hydrogen forwards the origin to the Storefront API.
string - url
The incoming request's URL.
string
Headers
NormalizedI18nConfig
The request's locale, with a path prefix that starts with `/` or is an empty string.
Omit<I18n, "pathPrefix"> & {
pathPrefix: string;
}ShopifyRouteTemplates
Your app's custom path templates for Shopify standard storefront routes, keyed by route name.
- article
The app's article path. Hydrogen redirects Shopify article paths, such as `/blogs/news/snowboard-guide`, to this template.
StandardRouteTemplateWithParam<"blogHandle"> & StandardRouteTemplateWithParam<"articleHandle"> - blog
The app's blog path. Hydrogen redirects Shopify blog paths, such as `/blogs/news`, to this template.
StandardRouteTemplateWithParam<"blogHandle"> - cart
The app's cart path. Hydrogen redirects Shopify's `/cart` path to this template.
StandardRouteTemplate - collection
The app's collection path. Hydrogen redirects Shopify collection paths, such as `/collections/winter`, to this template.
StandardRouteTemplateWithParam<"collectionHandle"> - collectionList
The app's collection list path. Hydrogen redirects Shopify's `/collections` path and the legacy `/products` path to this template.
StandardRouteTemplate - page
The app's page path. Hydrogen redirects Shopify page paths, such as `/pages/about-us`, to this template.
StandardRouteTemplateWithParam<"pageHandle"> - policy
The app's policy path. Hydrogen redirects Shopify policy paths, such as `/policies/privacy-policy`, to this template.
StandardRouteTemplateWithParam<"policyHandle"> - product
The app's product path. Hydrogen redirects Shopify product paths, such as `/products/snowboard`, to this template.
StandardRouteTemplateWithParam<"productHandle"> - productInCollection
The app's path for products inside a collection. Hydrogen redirects Shopify paths such as `/collections/winter/products/snowboard` to this template. The template must include `:productHandle`. Add `:collectionHandle` when the app's product path also names the collection.
StandardRouteTemplateWithParam<"productHandle"> - search
The app's search path. Hydrogen redirects Shopify's `/search` path to this template.
StandardRouteTemplate
StandardRouteTemplateWithParam
A path template that starts with `/` and includes the named handle placeholder.
`/${string}:${Param}` | `/${string}:${Param}/${string}`StandardRouteTemplate
A path template that starts with `/`.
`/${string}`ShopifyRouteSessionManager
The session storage that route handlers and the customer session read and write. Implement the methods with your app's session.
- commit
Saves session changes and returns the headers, such as a cookie, to add to the response.
() => Awaitable<void | HeadersInit> - getSessionItem
Returns the session value stored under a key.
(key: string) => unknown - getSessionOrigin
Returns your storefront's HTTPS origin. Customer account sign-in builds its OAuth redirect URL from the origin.
() => Awaitable<string> - removeSessionItem
Deletes the session value stored under a key.
(key: string) => Awaitable<void> - setSessionItem
Stores a value in the session under a key.
(key: string, value: unknown) => Awaitable<void>
Awaitable
A value or a promise for that value.
T | Promise<T>StorefrontClient
A Storefront API client. Call `graphql()` with a document to run a query or mutation and get a typed result.
- apiUrl
The Storefront API GraphQL endpoint that the client calls.
string - graphql
Runs a Storefront API query or mutation and returns a typed result. Pass a `gql` document or a plain query string. When the operation declares `$country` or `$language`, the client sets the variable from the request context and overrides any value you pass. The method throws an error when a query passes a `cache` strategy and the client has no cache, or when the strategy uses `private` mode. A catch block for `StorefrontApiError` doesn't catch this configuration error. To cache customer-specific data, use `createRunWithCache` with a key unique to the customer.
<const Doc extends DocLike | string>(doc: Doc, ...options: GqlRestParam<ResolveDoc<Doc>, Extra>) => Promise<StorefrontGraphqlResult<ResolveDoc<Doc>>> - i18n
The country and language that the client sends for `$country` and `$language` variables.
RequestContext["i18n"] - requestContext
The request context that you passed to `createStorefrontClient`.
RequestContext - storefrontId
The storefront ID from the client options.
string - storeUrl
Your store's URL with its protocol, such as `https://my-store.myshopify.com`.
string - type
The client type that you passed to `createStorefrontClient`.
Type
DocLike
A document that the client's GraphQL method accepts, from `gql` or from `gql`.tada.
TadaDocumentNode<any, any> | AnyStorefrontQueryStringAnyStorefrontQueryString
Any `gql` document, whatever its result and variables types. Use the type to accept any Storefront API document.
string & StorefrontQueryMetadataStorefrontQueryMetadata
Type-only properties that mark a string as a `gql` document.
- __hydrogenQueryBrand
Marks a string that `gql` returns. The property exists only in types.
true - __hydrogenQuerySource
The document's query text, which TypeScript reads to infer types. The property exists only in types.
Source
GqlRestParam
The options argument of a GraphQL call. Pass the options argument when the document declares a required variable. You can leave the options argument out when every variable is optional, or when the only required variables are `$country` and `$language`, which the client fills.
HasNoRequiredKeys<MergedOptions<Doc, GraphqlExtraOptionsForDoc<Doc, Extra>>> extends true
? [options?: MergedOptions<Doc, GraphqlExtraOptionsForDoc<Doc, Extra>>]
: [options: MergedOptions<Doc, GraphqlExtraOptionsForDoc<Doc, Extra>>]HasNoRequiredKeys
Resolves to `true` when an object type has no required keys.
Record<string, never> extends T ? true : falseMergedOptions
The options for a GraphQL call. The call needs `variables` only when the document declares a required variable.
Extra & StorefrontGraphqlOptions & (HasNoRequiredKeys<UserVariables<Doc>> extends true
? { variables?: UserVariables<Doc> }
: { variables: UserVariables<Doc> })StorefrontGraphqlOptions
Options that every Storefront API GraphQL call accepts.
- signal
Cancels the request when the signal aborts. The request context's signal and the client timeout also cancel the request. The signal doesn't cancel a stale-while-revalidate refresh.
AbortSignal
UserVariables
The variables that you pass for a document, without `country` and `language`. The client fills both from the request context.
VariablesOfDoc
The variables that a Customer Account document declares.
Doc extends CustomerAccountDocument<unknown, infer Variables, string> ? Variables : neverCustomerAccountDocument
A Customer Account API query or mutation that `gql` from `@shopify/hydrogen/customer-account` returns. TypeScript infers the result and variables types from the document's GraphQL source. Create documents only with the Customer Account `gql` function. The client throws a `TypeError` for any other object.
DocumentDecoration<Result, Variables> & {
readonly [CUSTOMER_ACCOUNT_DOCUMENT]: true;
readonly source: Source;
}GraphqlExtraOptionsForDoc
Offers the `cache` option only for documents that TypeScript recognizes as queries.
OperationKindOfDoc<Doc> extends "query" ? Extra : Omit<Extra, "cache">OperationKindOfDoc
The operation kind of a document, such as `query`. Resolves to `unknown` when the query text isn't a string literal type.
[SourceText<Doc>] extends [never]
? "unknown"
: string extends SourceText<Doc>
? "unknown"
: InferOperationKind<SourceText<Doc>>SourceText
The query text of a document as a string literal type. A plain string resolves to itself.
[SourceOf<Doc>] extends [never]
? Doc extends string
? Doc
: never
: SourceOf<Doc>InferOperationKind
The operation kind of a query string, such as `query`. Resolves to `unknown` unless the query text has exactly one operation.
parseDocument<T> extends infer Doc
? [Doc] extends [never]
? "unknown"
: OperationKindType<Doc>
: "unknown"OperationKindType
ResolveDoc
The typed document for a GraphQL call. Hydrogen infers the types when you pass a plain query string.
D extends DocLike ? D : D extends string ? InferredDoc<D> : neverInferredDoc
StorefrontGraphqlResult
The result of a GraphQL call, with the data, any GraphQL errors, and the response headers. Check `errors` before you read `data`. Without errors, `data` holds the full result, typed from your document. With errors, `data` is `null` after a request error or after a failed non-null field at the root. Otherwise, `data` holds a partial result, and `null` replaces each failed field or its nearest nullable parent.
{ data: ResultOfDoc<Doc>; errors?: undefined; headers: Headers } | { data: ResultOfDoc<Doc> | null; errors: GraphQLFormattedError[]; headers: Headers }ResultOfDoc
The result type of a `gql` document or a `gql`.tada document node.
Doc extends StorefrontQueryString<infer Result, infer _Variables, string>
? Result
: Doc extends TadaDocumentNode<any, any>
? TadaResultOf<Doc>
: neverStorefrontQueryString
GraphQLFormattedError
A GraphQL error from the Storefront API. The result of a GraphQL call lists GraphQL errors in its `errors` field.
- extensions
Extra details that the Storefront API adds to the error.
Record<string, unknown> - locations
The lines and columns in the query where the error occurs.
ReadonlyArray<{ line: number; column: number }> - message
The error message.
string - path
The path to the response field that failed.
ReadonlyArray<string | number>
TypeScript
Examples
Description
Build the request context and client, then serve Shopify's endpoints before the app router. This example falls through to the app router when no route matches.
TypeScript
import { createShopifyRequestContext, createStorefrontClient, handleShopifyRoutes, type ShopifyRouteHandlerContext, type ShopifyRouteHandlerGroup, type ShopifyRouteTemplates, type StorefrontClient, } from "@shopify/hydrogen"; export async function fetch( request: Request, appRouter: (request: Request, storefrontClient: StorefrontClient) => Promise<Response>, sessionManager: ShopifyRouteHandlerContext["sessionManager"], routeTemplates: ShopifyRouteTemplates, cartHandlers: ShopifyRouteHandlerGroup, ) { const requestContext = createShopifyRequestContext({ request, i18n: { country: "US", language: "EN" }, }); const storefrontClient = createStorefrontClient({ type: "public", requestContext, config: { storeDomain: process.env.PUBLIC_STORE_DOMAIN! }, }); const shopifyRoute = handleShopifyRoutes({ request, requestContext, sessionManager, storefrontClient, routeTemplates, handlers: [cartHandlers], }); if (shopifyRoute) return await shopifyRoute; const routedResponse = await appRouter(request, storefrontClient); const response = new Response(routedResponse.body, routedResponse); requestContext.applyResponseHeaders(response.headers); return response; }
Anchor to Custom endpointsCustom endpoints
Creates a custom endpoint in your app for one pathname and HTTP method. Collect handlers in an object, and pass an array of handler objects to in its handlers option.
Use paths other than , which the cart server handlers use. Return a JSON, redirect, or error result from the handler. Hydrogen sends a JSON result with status 200, an error result with status 400 by default, and a redirect result with status 303 by default. When a redirect result uses a status other than 301, 302, 303, 307, or 308, the promise from rejects.
- Anchor to pathnamepathnamepathnameTPathname extends stringTPathname extends stringrequiredrequired
The exact URL pathname that the handler matches. Hydrogen doesn't match path parameters or prefixes.
- Anchor to methodmethodmethodTMethod extends stringTMethod extends stringrequiredrequired
The HTTP method that the handler matches, in uppercase, such as
POST.- Anchor to handlerhandlerhandler(context: ShopifyRouteHandlerContext) => Promise<ShopifyRouteHandlerResult>(context: ShopifyRouteHandlerContext) => Promise<ShopifyRouteHandlerResult>requiredrequired
The function that receives the request, its context, the session, and the Storefront API client, and returns a JSON, redirect, or error result.
The handler to add to a handler object.
ShopifyRouteHandlerContext
The values that Hydrogen passes to each registered route handler.
- request
The incoming request that matched the handler's pathname and method.
Request - requestContext
The current request's context, the same one the Storefront API client uses.
ShopifyRequestContext - sessionManager
Your app's session storage for the current request.
ShopifyRouteSessionManager - storefrontClient
The Storefront API client for the current request.
StorefrontClient
ShopifyRouteHandlerResult
The JSON, redirect, or error result that a route handler returns.
ShopifyRouteJsonResult<TData> | ShopifyRouteRedirectResult | ShopifyRouteErrorResult<TError>ShopifyRouteJsonResult
A route handler result that Hydrogen sends as a `200` JSON response.
- data
The value that Hydrogen serializes as the response body.
TData - headers
Extra headers for the response. Hydrogen sets `content-type` to `application/json` and overrides any value you pass.
HeadersInit - type
Set to `json` for a JSON response.
'json'
ShopifyRouteRedirectResult
A route handler result that Hydrogen sends as a redirect response.
- headers
Extra headers for the redirect response.
HeadersInit - location
The redirect target. Hydrogen resolves a relative path against the request's origin.
string - status
The HTTP redirect status. Defaults to `303`.
ShopifyRedirectStatus - type
Set to `redirect` for a redirect response.
'redirect'
ShopifyRedirectStatus
The HTTP status codes a route handler can use for a redirect.
301 | 302 | 303 | 307 | 308ShopifyRouteErrorResult
A route handler result that Hydrogen sends as a JSON error response.
- error
The error that Hydrogen serializes as the response body's `error` field.
TError - headers
Extra headers for the error response. Hydrogen sets `content-type` to `application/json` and overrides any value you pass.
HeadersInit - status
The HTTP status for the error response. Defaults to `400`.
number - type
Set to `error` for an error response.
'error'
Anchor to shopifyrouteerrorShopifyRouteError
The code and message that an error result sends in the response body's error field.
- Anchor to codecodecodestringstringrequiredrequired
A machine-readable error code.
- Anchor to messagemessagemessagestringstringrequiredrequired
A human-readable error message.
TypeScript
Examples
Description
Create a GET route that queries the Storefront API. This example returns a `json` result on success and an `error` result with status `502` when the query fails.
TypeScript
import { createShopifyRouteHandler, gql, type ShopifyRouteHandlerContext, type ShopifyRouteHandlerResult, } from "@shopify/hydrogen"; const SHOP_QUERY = gql(`query { shop { name } }`); async function handleShopGet({ storefrontClient, }: ShopifyRouteHandlerContext): Promise<ShopifyRouteHandlerResult> { const result = await storefrontClient.graphql(SHOP_QUERY); if (result.errors) { return { type: "error", status: 502, error: { code: "storefront_error", message: result.errors[0]?.message ?? "Request failed" }, }; } return { type: "json", data: { name: result.data.shop.name } }; } export const shopHandlers = { get: createShopifyRouteHandler("/api/shop", "GET", handleShopGet), };Description
Return a redirect result from a handler. This example sends `/wishlist` to `/account/wishlist` with a permanent redirect.
TypeScript
import { createShopifyRouteHandler } from "@shopify/hydrogen"; export const legacyHandlers = { wishlist: createShopifyRouteHandler("/wishlist", "GET", async () => ({ type: "redirect" as const, location: "/account/wishlist", status: 301 as const, })), };