|
| 1 | +# Grid API TypeScript Sample |
| 2 | + |
| 3 | +A sample application demonstrating the Grid API payout flow using the [Grid TypeScript SDK](https://www.npmjs.com/package/@lightsparkdev/grid). |
| 4 | + |
| 5 | +## What It Does |
| 6 | + |
| 7 | +This sample walks through a complete payout: |
| 8 | + |
| 9 | +1. **Create Customer** — Register an individual customer on the platform |
| 10 | +2. **Create External Account** — Link a USD bank account to the customer |
| 11 | +3. **Create Quote** — Get a real-time quote for USDC to USD conversion |
| 12 | +4. **Sandbox Fund** — Simulate funding to complete the transaction |
| 13 | + |
| 14 | +Webhook events are streamed to the frontend in real time via Server-Sent Events (SSE). |
| 15 | + |
| 16 | +## Prerequisites |
| 17 | + |
| 18 | +- **Node.js 18+** |
| 19 | +- **Grid API sandbox credentials** (Reach out to your contact at lightspark or sales@lightspark.com to get them) |
| 20 | + |
| 21 | +## Setup |
| 22 | + |
| 23 | +1. Copy the environment template: |
| 24 | + |
| 25 | + ```bash |
| 26 | + cp .env.example .env |
| 27 | + ``` |
| 28 | + |
| 29 | +2. Fill in your credentials in `.env`: |
| 30 | + |
| 31 | + ```bash |
| 32 | + GRID_API_TOKEN_ID=your_api_token_id |
| 33 | + GRID_API_CLIENT_SECRET=your_api_client_secret |
| 34 | + ``` |
| 35 | + |
| 36 | +## Running |
| 37 | + |
| 38 | +### Option 1: Single command (recommended) |
| 39 | + |
| 40 | +Builds the frontend, then starts the server: |
| 41 | + |
| 42 | +```bash |
| 43 | +cd samples/typescript |
| 44 | +npm install |
| 45 | +npm start |
| 46 | +``` |
| 47 | + |
| 48 | +Open [http://localhost:8080](http://localhost:8080) in your browser. |
| 49 | + |
| 50 | +### Option 2: Separate dev servers (for frontend development) |
| 51 | + |
| 52 | +If you're iterating on the frontend, run them separately for hot reload: |
| 53 | + |
| 54 | +**Terminal 1 — Backend (port 8080):** |
| 55 | + |
| 56 | +```bash |
| 57 | +cd samples/typescript |
| 58 | +npm install |
| 59 | +npm run dev |
| 60 | +``` |
| 61 | + |
| 62 | +**Terminal 2 — Frontend (port 5173):** |
| 63 | + |
| 64 | +```bash |
| 65 | +cd samples/frontend |
| 66 | +npm install |
| 67 | +npm run dev |
| 68 | +``` |
| 69 | + |
| 70 | +Open [http://localhost:5173](http://localhost:5173) in your browser. The Vite dev server proxies `/api` requests to the backend. |
| 71 | + |
| 72 | +## Webhook Setup |
| 73 | + |
| 74 | +To receive webhooks locally, expose your backend with a tunnel: |
| 75 | + |
| 76 | +```bash |
| 77 | +ngrok http 8080 |
| 78 | +``` |
| 79 | + |
| 80 | +Then configure the webhook URL in your [Grid dashboard](https://app.lightspark.com) as: |
| 81 | + |
| 82 | +``` |
| 83 | +https://<your-ngrok-id>.ngrok.io/api/webhooks |
| 84 | +``` |
| 85 | + |
| 86 | +## Architecture |
| 87 | + |
| 88 | +``` |
| 89 | +Browser (React) → Vite Dev Server (:5173) → Express Backend (:8080) → Grid API |
| 90 | + proxy /api Grid TypeScript SDK |
| 91 | + ↑ |
| 92 | + Grid Webhooks (POST) |
| 93 | + ↓ |
| 94 | + SSE stream → Browser |
| 95 | +``` |
| 96 | + |
| 97 | +The backend is a thin proxy — it holds your API credentials and translates JSON requests into Grid SDK calls. The frontend handles the step-by-step wizard flow. |
| 98 | + |
| 99 | +## Sample Requests |
| 100 | + |
| 101 | +Grid API calls are in `src/routes/`: |
| 102 | + |
| 103 | +| File | Description | |
| 104 | +|------|-------------| |
| 105 | +| [`customers.ts`](src/routes/customers.ts) | Create a customer via `client.customers.create()` | |
| 106 | +| [`externalAccounts.ts`](src/routes/externalAccounts.ts) | Link a bank account via `client.customers.externalAccounts.create()` | |
| 107 | +| [`quotes.ts`](src/routes/quotes.ts) | Create and execute a quote via `client.quotes.create()` / `client.quotes.execute()` | |
| 108 | +| [`sandbox.ts`](src/routes/sandbox.ts) | Simulate funding via `client.sandbox.sendFunds()` | |
| 109 | +| [`webhooks.ts`](src/routes/webhooks.ts) | Receive incoming webhooks | |
| 110 | + |
| 111 | +Client initialization is in [`gridClient.ts`](src/gridClient.ts). |
0 commit comments