# How to use hosted checkout (https://developer.godaddy.com/en/docs/api-users/commerce/accept-payments/online-payments/hosted)

---
title: How to use hosted checkout
description: >-
  Create a checkout session on your server, then redirect the shopper to the
  GoDaddy-hosted checkout URL.
keywords: >-
  hosted checkout, Unified Checkout, redirect checkout, session url, returnUrl,
  successUrl, createCheckoutSession, Personal Access Token, GODADDY_PAT,
  /c/sessionId, website payments, GoDaddy Payments.
agentNotes:
  permissions:
    - GoDaddy Payments account on the business that owns the store
  scopes:
    - 'commerce.checkout-session:write'
  idempotent: false
  destructive: false
  failureRecovery: >-
    Distinguish two failures. A failure creating the session means the
    credential, the storeId, the order, or the return URLs are wrong, and no
    money has moved. A failure after a valid session exists is a payment failure
    that GoDaddy Payments reports on the hosted page. Session creation isn't
    idempotent, so calling it again creates another session and another draft
    order. Reuse the session url you already have.
related:
  guides:
    - title: How to create a checkout session
      href: /docs/api-users/commerce/accept-payments/online-payments/create-session
    - title: How to render embedded checkout
      href: /docs/api-users/commerce/accept-payments/online-payments/embedded
    - title: How to configure checkout
      href: /docs/api-users/commerce/accept-payments/online-payments/configure
    - title: How to take your first payment
      href: /docs/api-users/commerce/accept-payments/first-payment
  concepts:
    - title: About online payments
      href: /docs/api-users/commerce/accept-payments/online-payments
    - title: Checkout session reference
      href: /docs/api-users/commerce/accept-payments/online-payments/session-fields
pageId: a2c2f96d-db37-4a5b-bd1b-65ae4d3ae5e8
---

## Overview

Hosted checkout sends the shopper to a checkout page that GoDaddy hosts. Your server creates a checkout session with `createCheckoutSession`, returns the session `url`, and your page redirects the shopper to that URL.

The hosted page is the `/c/:sessionId` entrypoint. The session id is in the path and the session token is in the URL fragment (`#token`). That route doesn't create a session. It loads a session your server already created. Use the `url` the session returns and don't construct it yourself. The fragment carries the session token so it doesn't reach intermediate servers as a query parameter.

This page covers the redirect flow. Go to [How to render embedded checkout](https://developer.godaddy.com/docs/api-users/commerce/accept-payments/online-payments/embedded) if the shopper must stay on your site. Go to [About online payments](https://developer.godaddy.com/docs/api-users/commerce/accept-payments/online-payments#when-you-learn-the-result) for how you confirm the payment after the shopper returns.

## Prerequisites

The following prerequisites are required before you can redirect a shopper to hosted checkout:

* a server route that creates a checkout session and returns `{ url: session.url }`

Go to [How to create a checkout session](https://developer.godaddy.com/docs/api-users/commerce/accept-payments/online-payments/create-session) to build the server route.

## Redirect the shopper

The following procedure fetches the session and sends the shopper to hosted checkout.

1. Fetch the session from your endpoint:

   ```typescript
   const response = await fetch('/api/checkout-session', { method: 'POST' });
   const data = await response.json();

   if (!response.ok) {
     throw new Error(data.error ?? 'Failed to create checkout session');
   }

   if (!data.url) {
     throw new Error('No checkout URL returned from API');
   }
   ```

2. Go to the session URL:

   ```typescript
   window.location.href = data.url;
   ```

Return `Response.redirect(session.url, 302)` (or `res.redirect(302, session.url)` in Express) immediately after `createCheckoutSession` and skip the fetch-and-redirect steps.

## Handle success and cancel

After a successful payment, the shopper goes to your `successUrl`. After a cancel, the shopper goes to your `returnUrl`.

* Serve public HTTPS pages at both URLs. Checkout rejects loopback URLs at session create time, so local development needs a tunnel.
* Checkout stores `successUrl` exactly as you sent it and expands nothing in it. The URL has to be final before you create the session.
* To carry an order ID in `successUrl`, create the draft order first and pass `draftOrderId`. Using `lineItems` creates the order during session create, so no ID exists before you call.
* A declined or failed payment stays on the hosted checkout page, where the shopper might retry.

Go to [How to create a checkout session](https://developer.godaddy.com/docs/api-users/commerce/accept-payments/online-payments/create-session#common-errors) for session create error messages and what to change.
