GoDaddy

How to render embedded checkout

View as Markdown

Render Unified Checkout inside your own website with a React component, using a session your server creates.

Overview

Embedded checkout keeps the shopper on your own website. A React component renders the checkout interface in your page, and your server creates the checkout session.

The integration has three parts: a server route that creates the session, a page that fetches it, and a Checkout component that renders the form. The following diagram shows how they connect and which one holds your Personal Access Token (PAT).

Rendering diagram...

The package has a server half and a browser half. Your server imports createCheckoutSession from @godaddy/react/server. Your page imports GoDaddyProvider and Checkout from @godaddy/react. Your PAT never crosses into the browser.

This page covers the embedded form. Go to About online payments for how the payment completes.

Prerequisites

The following prerequisites are required before you can render embedded checkout:

  • a React 18 or 19 application
  • a server route that creates a checkout session and returns the whole session object, including its token

Go to How to create a checkout session to build the server route.

Add Unified Checkout to your project

The following procedure installs the checkout package and its stylesheet.

  1. Install the package:

    npm install @godaddy/react @tanstack/react-query react-hook-form
  2. Import the stylesheet once at your application entry point:

    import '@godaddy/react/styles-tailwind.css';

    The checkout interface doesn't load its styles without it.

@tanstack/react-query and react-hook-form are peer dependencies the checkout component needs. Projects that don't use Tailwind import @godaddy/react/styles.css instead.

Wrap your app in the provider

Every prop is optional. The following table lists when to use each one:

PropWhy supply itNote
queryClientTo share a query cache and retry settings with the rest of your appThe provider creates its own if you omit this
apiHostTo route checkout API calls through a different hostDefaults to api.godaddy.com; session create uses a separate host
storeId, clientId, channelIdTo make these identifiers available without reading the sessionThe session already carries these (never put a secret here)
appearanceTo match checkout colors to your brand
localeTo force a specific language regardless of browser settingsDefaults to en-US
localizationTo override individual strings in the checkout formTypes come from @godaddy/localizations, which installs with the package
debugTo log verbose checkout events during development
LinkTo navigate checkout links client-side with your router

These values run in the browser, so read them through your bundler's public environment mechanism. Server-only variables aren't available here.

The following procedure sets up GoDaddyProvider once, wrapping your routes.

  • Create a providers component:

    'use client';
    
    import { GoDaddyProvider } from '@godaddy/react';
    import { QueryClient } from '@tanstack/react-query';
    import { useState } from 'react';
    
    export function Providers({ children }: { children: React.ReactNode }) {
      // Create the client inside the component so server rendering
      // doesn't share one client across requests.
      const [queryClient] = useState(
        () =>
          new QueryClient({
            defaultOptions: {
              queries: { retry: 1, refetchOnWindowFocus: false },
            },
          }),
      );
    
      return (
        <GoDaddyProvider apiHost="api.godaddy.com" queryClient={queryClient}>
          {children}
        </GoDaddyProvider>
      );
    }

Render checkout

The <Checkout> component takes the session your server created. The following table lists common props and what they do.

PropPurposeNote
layoutControls the order sections display inCheckout appends any you omit after the ones you list. The session decides which sections display, not layout.
checkoutFormSchemaApplies a zod schema for extra field validation
defaultValuesPre-fills form fieldsOnly contactEmail is supported
appearanceSets theme variables for the checkout formOverrides values set on the provider. Session appearance values override this prop.
enableTrackingEmits checkout analytics events

Don't list a section the session hasn't enabled

layout normally can't make a section display. tips is the exception: listing it while enableTips is unset reserves its row anyway, leaving a visible gap. List tips only when the session enables it.

The following procedure renders the checkout form with optional customizations for layout order and field validation.

  1. Fetch the session from your endpoint:

    import type { CheckoutSession } from '@godaddy/react';
    import { useEffect, useState } from 'react';
    
    export default function Home() {
      const [session, setSession] = useState<CheckoutSession | null>(null);
      const [error, setError] = useState<string | null>(null);
    
      useEffect(() => {
        fetch('/api/checkout-session', { method: 'POST' })
          .then(async (response) => {
            const data = await response.json();
            if (!response.ok) throw new Error(data.error ?? 'Could not create the session');
            return data;
          })
          .then(setSession)
          .catch((error) => setError(error.message));
      }, []);
    
      if (error) return <p>{error}</p>;
      if (!session) return <p>Creating checkout session…</p>;
    
      return <CheckoutPage session={session} />;
    }
  2. Render the checkout component:

    import { Checkout, type CheckoutSession } from '@godaddy/react';
    
    export function CheckoutPage({ session }: { session: CheckoutSession }) {
      return (
        <Checkout session={session} />
      );
    }
  3. (Optional) Order the checkout sections by passing layout:

    <Checkout
      session={session}
      layout={['payment', 'contact']}
    />

    Valid values are contact, payment, shipping, pickup, delivery, tips, express-checkout, and notes. Checkout appends any you omit after the ones you list.

  4. (Optional) Require extra fields with a zod schema:

    import { z } from 'zod';
    import type { CheckoutFormSchema } from '@godaddy/react';
    
    const customSchema: CheckoutFormSchema = {
      shippingPhone: z.string().min(1, 'Phone number is required'),
    };

    Then pass the schema to the component:

    <Checkout
      session={session}
      checkoutFormSchema={customSchema}
    />

    zod is included

    zod ships as a dependency of @godaddy/react. Install it directly if your package manager doesn't hoist dependencies.

A declined or failed payment shows an error in the checkout form, and the shopper might retry there.

Go to Checkout session reference for which sections display and what session flags enable them.

Go to About online payments for how you confirm the payment after the shopper returns.

Go to How to create a checkout session for session create error messages and what to change.

Last updated on

How is this guide?

On this page