Integration Guide · React

Build a React Ecommerce Application

Connect a React (Vite, Parcel, or custom bundler) frontend directly to Shoprivet public Storefront endpoints and scoped guest cart sessions.

Target: React 18+ / 19+ (Vite or SPA) Integration: Public Storefront REST API (browser) + optional backend proxy for /v1 admin actions OpenAPI 3.0 Spec

Prerequisites & Setup Checklist

  • React 18+ or 19+
  • Shoprivet store slug
  • Connected payment gateway or Cash on delivery
  • Sign up at shoprivet.io/app/signup, create a store, add products, and configure a payment gateway (or Cash on delivery).

Environment Variables

Variable Example Value Exposure Rule
VITE_SHOPRIVET_STORE_SLUG downtown-shop Safe public store slug
SHOPRIVET_API_KEY shoprivet_live_example_key Backend ONLY (never use VITE_SHOPRIVET_API_KEY)

Implementation Steps

1. Load catalog and payment methods from public routes

Because a pure React SPA executes in the visitor browser, use /public/stores/:slug endpoints for fetching products, categories, and payment methods rather than merchant API keys.

2. Manage guest cart credentials in state or sessionStorage

Create a cart once when a shopper adds their first item, store the returned bearer credential in localStorage or sessionStorage, and clear it after checkout succeeds.

3. Verify payment on the return route

Store checkout.orderAccessToken briefly in sessionStorage before navigating to checkout.authorizationUrl. When the gateway redirects back, call POST /public/stores/:slug/orders/:orderNumber/confirm.

src/hooks/useShoprivetCatalog.ts · Fetch active products and payment methods
import { useEffect, useState } from 'react';

const API = 'https://api.shoprivet.io';
const SLUG = import.meta.env.VITE_SHOPRIVET_STORE_SLUG || 'downtown-shop';

export function useShoprivetCatalog() {
  const [products, setProducts] = useState<any[]>([]);
  const [methods, setMethods] = useState<any[]>([]);
  const [error, setError] = useState<string | null>(null);

  useEffect(() => {
    Promise.all([
      fetch(`${API}/public/stores/${SLUG}/products?page=1&limit=24`).then((r) => r.json()),
      fetch(`${API}/public/stores/${SLUG}/payment-methods`).then((r) => r.json()),
    ])
      .then(([catalog, payments]) => {
        setProducts(catalog.products || []);
        setMethods(payments.methods || []);
      })
      .catch((err) => setError(err instanceof Error ? err.message : 'Failed to load store'));
  }, []);

  return { products, methods, error };
}
src/lib/confirmOrder.ts · Confirm payment after gateway redirect
export async function confirmShoprivetOrder(slug: string, orderNumber: string, token: string, reference?: string) {
  const res = await fetch(`https://api.shoprivet.io/public/stores/${slug}/orders/${orderNumber}/confirm`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      Authorization: `Bearer ${token}`,
    },
    body: JSON.stringify(reference ? { reference } : {}),
  });
  if (!res.ok) {
    const err = await res.json().catch(() => ({}));
    throw new Error(err.message || 'Payment verification failed');
  }
  return res.json();
}

Payment Verification, Error Handling & Security

React SPAs render content client-side unless pre-rendered. If organic search indexing for individual product pages matters, consider Next.js or Astro SSR, or use Shoprivet hosted store URLs alongside your SPA.

  • Never create VITE_SHOPRIVET_API_KEY or REACT_APP_SHOPRIVET_API_KEY; bundler prefixes expose environment variables publicly.
  • Handle HTTP 401/410 on expired guest cart tokens by calling POST /public/stores/:slug/carts to issue a fresh session.

Deployment

Build with npm run build / pnpm build and deploy to Cloudflare Pages, Vercel, Netlify, or any static host.