Express.js Integration
Complete example of integrating Easy SDK with an Express.js server.
Basic Setup
import express from "express";
import { createClient } from "@easylabs/node";
import dotenv from "dotenv";
dotenv.config();
const app = express();
app.use(express.json());
// Initialize Easy client
const easy = await createClient({
apiKey: process.env.EASY_API_KEY!,
});
app.listen(3000, () => {
console.log("Server running on port 3000");
});
Customer Routes
// Get all customers
app.get("/api/customers", async (req, res) => {
try {
const { limit = 10, offset = 0 } = req.query;
const customers = await easy.getCustomers({
limit: Number(limit),
offset: Number(offset),
});
res.json(customers);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Get single customer
app.get("/api/customers/:id", async (req, res) => {
try {
const customer = await easy.getCustomer(req.params.id);
res.json(customer);
} catch (error) {
res.status(404).json({ error: error.message });
}
});
// Create customer
app.post("/api/customers", async (req, res) => {
try {
const customer = await easy.createCustomer({
first_name: req.body.firstName,
last_name: req.body.lastName,
email: req.body.email,
phone: req.body.phone,
personal_address: req.body.address,
});
res.status(201).json(customer);
} catch (error) {
res.status(400).json({ error: error.message });
}
});
// Update customer
app.patch("/api/customers/:id", async (req, res) => {
try {
const updated = await easy.updateCustomer(req.params.id, req.body);
res.json(updated);
} catch (error) {
res.status(400).json({ error: error.message });
}
});
Payment Routes
// Create payment instrument
app.post("/api/payment-instruments", async (req, res) => {
try {
const instrument = await easy.createPaymentInstrument({
type: req.body.type,
name: req.body.name,
identityId: req.body.customerId,
tokenId: req.body.tokenId, // From client-side tokenization
address: req.body.address,
});
res.status(201).json(instrument);
} catch (error) {
res.status(400).json({ error: error.message });
}
});
// Process payment
app.post("/api/payments", async (req, res) => {
try {
const transfer = await easy.createTransfer({
amount: req.body.amount,
currency: req.body.currency || "USD",
source: req.body.paymentInstrumentId,
statement_descriptor: req.body.description,
tags: req.body.metadata,
});
res.status(201).json(transfer);
} catch (error) {
res.status(400).json({ error: error.message });
}
});
// Get payment status
app.get("/api/payments/:id", async (req, res) => {
try {
const transfer = await easy.getTransfer(req.params.id);
res.json(transfer);
} catch (error) {
res.status(404).json({ error: error.message });
}
});
Checkout Route
app.post("/api/checkout", async (req, res) => {
try {
const { isNewCustomer, customerData, customerId, paymentToken, lineItems } =
req.body;
const checkoutData = {
customer_creation: isNewCustomer,
...(isNewCustomer
? {
customer_details: {
first_name: customerData.firstName,
last_name: customerData.lastName,
email: customerData.email,
phone: customerData.phone,
personal_address: customerData.address,
},
}
: {
identity_id: customerId,
}),
source: paymentToken
? {
type: "PAYMENT_CARD",
tokenId: paymentToken,
name: "Customer Card",
}
: req.body.paymentInstrumentId,
line_items: lineItems,
metadata: {
order_source: "api",
...req.body.metadata,
},
};
const result = await easy.checkout(checkoutData);
res.status(201).json({
success: true,
order: result.data,
});
} catch (error) {
res.status(500).json({
success: false,
error: error.message,
});
}
});
Product Routes
// Get all products
app.get("/api/products", async (req, res) => {
try {
const products = await easy.getProducts();
res.json(products);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Get product with prices
app.get("/api/products/:id", async (req, res) => {
try {
const product = await easy.getProductWithPrices(req.params.id);
res.json(product);
} catch (error) {
res.status(404).json({ error: error.message });
}
});
// Create product
app.post("/api/products", async (req, res) => {
try {
const product = await easy.createProduct(req.body);
res.status(201).json(product);
} catch (error) {
res.status(400).json({ error: error.message });
}
});
// Create price
app.post("/api/prices", async (req, res) => {
try {
const price = await easy.createPrice(req.body);
res.status(201).json(price);
} catch (error) {
res.status(400).json({ error: error.message });
}
});
Error Handling Middleware
// Global error handler
app.use((err, req, res, next) => {
console.error("Error:", err);
res.status(err.status || 500).json({
error: err.message || "Internal server error",
});
});
Complete Server Example
import express from "express";
import { createClient } from "@easylabs/node";
import dotenv from "dotenv";
import cors from "cors";
dotenv.config();
const app = express();
// Middleware
app.use(cors());
app.use(express.json());
// Initialize Easy client
const easy = await createClient({
apiKey: process.env.EASY_API_KEY!,
});
// Health check
app.get("/health", (req, res) => {
res.json({ status: "ok" });
});
// API routes
app.get("/api/customers", async (req, res) => {
const customers = await easy.getCustomers();
res.json(customers);
});
app.post("/api/customers", async (req, res) => {
const customer = await easy.createCustomer(req.body);
res.status(201).json(customer);
});
app.post("/api/checkout", async (req, res) => {
const result = await easy.checkout(req.body);
res.status(201).json(result);
});
// Error handling
app.use((err, req, res, next) => {
console.error(err);
res.status(500).json({ error: err.message });
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});