Skip to main content
The customer service module (app/services/customer.ts) wraps the Stripe SDK’s customer API. It initializes a single Stripe client and exposes functions for customer lifecycle management. All functions are async and designed to run server-side inside React Router loaders and actions.
The Stripe client is initialized with a hardcoded test key in the source. Replace sk_test_... with your actual key via an environment variable before deploying. See Environment configuration.

Functions

createCustomer

Creates a new Stripe customer with the provided email and display name.
export async function createCustomer(email: string, name: string): Promise<Stripe.Customer>
Internally calls stripe.customers.create() with email and name, then returns the full Stripe.Customer object that Stripe resolves. Parameters
email
string
required
The customer’s email address. Stripe uses this for receipts, invoice emails, and customer search.
name
string
required
The customer’s full display name. Shown in the Stripe Dashboard and on invoices.
Return value Returns Promise<Stripe.Customer>. Key fields on the resolved object:
id
string
required
Unique Stripe customer identifier, prefixed with cus_. Store this in the session as customerId to identify the customer in subsequent API calls.
email
string
The email address provided at creation time.
name
string
The display name provided at creation time.
created
number
Unix timestamp (seconds) when the customer was created.
livemode
boolean
true when the customer was created in live mode, false in test mode. Always false when using a sk_test_ key.
Example usage The registration action in app/routes/register.tsx calls createCustomer after validating the form, then stores the returned id in the session:
register action
import { createCustomer } from "~/services/customer";
import { getSession, commitSession } from "~/sessions";

export async function action({ request }: Route.ActionArgs) {
  const formData = await request.formData();
  const email = String(formData.get("email"));
  const name = String(formData.get("name"));

  // Create the Stripe customer
  const customer = await createCustomer(email, name);

  // Persist the customer ID in the encrypted session cookie
  const session = await getSession(request.headers.get("Cookie"));
  session.set("customerId", customer.id);

  return redirect("/prices", {
    headers: { "Set-Cookie": await commitSession(session) },
  });
}
You only need to call createCustomer once per user. After the customer is created, read customerId from the session cookie for all subsequent Stripe calls (subscribe, listSubscriptions, etc.).

Build docs developers (and LLMs) love