Skip to main content

createCustomerClient

Caution

This component is in an unstable pre-release state and may have breaking changes in a future release.

The createCustomerClient function creates a GraphQL client for querying the Customer Account API. It also provides methods to authenticate and check if the user is logged in.

See an end to end example on using the Customer Account API client.

Anchor to input1
input1
required

CustomerClient

login
() => Promise<Response>

Start the OAuth login flow. This function should be called and returned from a Remix action. It redirects the user to a login domain.

authorize
(redirectPath?: string) => Promise<Response>

On successful login, the user is redirect back to your app. This function validates the OAuth response and exchanges the authorization code for an access token and refresh token. It also persists the tokens on your session. This function should be called and returned from the Remix loader configured as the redirect URI within the Customer Account API settings.

isLoggedIn
() => Promise<boolean>

Returns if the user is logged in. It also checks if the access token is expired and refreshes it if needed.

logout
() => Promise<Response>

Logout the user by clearing the session and redirecting to the login domain. It should be called and returned from a Remix action.

query
<ReturnType = any, RawGqlString extends string = string>(query: RawGqlString, options?: { variables: Record<string, any>; }) => Promise<ReturnType>

Execute a GraphQL query against the Customer Account API. Usually you should first check if the user is logged in before querying the API.

mutate
<ReturnType = any, RawGqlString extends string = string>(mutation: RawGqlString, options?: { variables: Record<string, any>; }) => Promise<ReturnType>

Execute a GraphQL mutation against the Customer Account API. Usually you should first check if the user is logged in before querying the API.

Examples
import {createCustomerClient__unstable} from '@shopify/hydrogen';
import * as remixBuild from '@remix-run/dev/server-build';
import {
createRequestHandler,
createCookieSessionStorage,
} from '@shopify/remix-oxygen';

export default {
async fetch(request, env, executionContext) {
const session = await HydrogenSession.init(request, [env.SESSION_SECRET]);

/* Create a Customer API client with your credentials and options */
const customer = createCustomerClient__unstable({
/* Runtime utility in serverless environments */
waitUntil: (p) => executionContext.waitUntil(p),
/* Public Customer Account API token for your store */
customerAccountId: env.PUBLIC_CUSTOMER_ACCOUNT_ID,
/* Public account URL for your store */
customerAccountUrl: env.PUBLIC_CUSTOMER_ACCOUNT_URL,
request,
session,
});

const handleRequest = createRequestHandler({
build: remixBuild,
mode: process.env.NODE_ENV,
/* Inject the customer account client in the Remix context */
getLoadContext: () => ({customer}),
});

return handleRequest(request);
},
};

class HydrogenSession {
static async init(request, secrets) {
const storage = createCookieSessionStorage({
cookie: {
name: 'session',
httpOnly: true,
path: '/',
sameSite: 'lax',
secrets,
},
});

const session = await storage.getSession(request.headers.get('Cookie'));

return new this(storage, session);
}

get(key) {
return this.session.get(key);
}

destroy() {
return this.sessionStorage.destroySession(this.session);
}

flash(key, value) {
this.session.flash(key, value);
}

unset(key) {
this.session.unset(key);
}

set(key, value) {
this.session.set(key, value);
}

commit() {
return this.sessionStorage.commitSession(this.session);
}
}
Was this page helpful?