Node.js Integration
Integrate Yeld with your Node.js backend.
Backend Integration
Use Node.js for server-side operations and webhook handling
1
Node.js Integration Overview
Integrate Yeld with your Node.js backend for server-side operations like creating checkout links, managing businesses, and processing webhooks.
**Key Features:**
- Server-side API calls
- Database operations
- Webhook handlers
- Background job processing2
Setup
**Install Dependencies:**
```bash
npm install @supabase/supabase-js drizzle-orm postgres
```
**Configure Environment:**
```env
# .env
DATABASE_URL=postgresql://user:pass@host:5432/db
EVM_MASTER_MNEMONIC=your_mnemonic
SUPABASE_URL=https://your-project.supabase.co
SUPABASE_SERVICE_ROLE_KEY=your_service_key
```
**Initialize Database:**
```typescript
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import { schema } from "./db/schema";
const client = postgres(process.env.DATABASE_URL!);
export const db = drizzle(client, { schema });
```3
Creating Checkout Links
**Server-Side Checkout Link Creation:**
```typescript
import { createCheckoutLink } from "@/lib/checkout-links";
async function createPaymentLink() {
const result = await createCheckoutLink("biz_abc123", {
planName: "Service Package",
plan: "service-package",
description: "Full service package",
price: "299.00",
billing: "one-time",
});
if (result.ok) {
console.log("Checkout link created!");
// Send link to customer, etc.
} else {
console.error("Failed to create link:", result.error);
}
}
```
**Using Express Route:**
```typescript
import express from "express";
const app = express();
app.post("/api/create-checkout", async (req, res) => {
const { bizId, planName, price } = req.body;
const result = await createCheckoutLink(bizId, {
planName,
price,
billing: "one-time",
});
if (result.ok) {
res.json({ success: true });
} else {
res.status(400).json({ error: result.error });
}
});
```4
Webhook Handler
**Express Webhook Endpoint:**
```typescript
import express from "express";
import { createHmac } from "crypto";
const app = express();
app.use(express.raw({ type: "application/json" }));
app.post("/webhook/yeld", (req, res) => {
const signature = req.headers["x-yeld-signature"] as string;
const payload = req.body;
// Verify signature
const expected = createHmac("sha256", process.env.YELD_WEBHOOK_SECRET!)
.update(payload)
.digest("hex");
if (signature !== expected) {
return res.status(400).send("Invalid signature");
}
const event = JSON.parse(payload.toString());
// Handle events
switch (event.type) {
case "payment.confirmed":
console.log(`Payment confirmed: ${event.data.txHash}`);
// Update database, send confirmation, etc.
break;
case "payment.failed":
console.log(`Payment failed: ${event.data.error}`);
break;
}
res.json({ received: true });
});
```