Integrate with Node.js
Build a simple Express.js API endpoint that generates PDF documents using the Rynko SDK.
Prerequisites
- Node.js 18 or higher
- A Rynko account with an API key
- A published template (note the template ID)
Step 1: Set Up Your Project
Create a new project and install dependencies:
mkdir rynko-node-demo && cd rynko-node-demo
npm init -y
npm install @rynko/sdk express dotenv
Create a .env file with your API key:
.env
RYNKO_API_KEY=fm_abc123xyz456...
PORT=4000
warning
Never commit your .env file to version control. Add it to .gitignore.
Step 2: Initialize the Rynko Client
Create a shared client instance that can be imported across your application:
rynko.js
import 'dotenv/config';
import { Rynko } from '@rynko/sdk';
export const rynko = new Rynko({
apiKey: process.env.RYNKO_API_KEY,
});
Step 3: Create the Express Server
Build an endpoint that accepts invoice data and returns a PDF download URL:
server.js
import 'dotenv/config';
import express from 'express';
import { rynko } from './rynko.js';
const app = express();
app.use(express.json());
app.post('/api/invoices/pdf', async (req, res) => {
const { customerName, invoiceNumber, items, total } = req.body;
try {
// Queue document generation
const job = await rynko.documents.generatePdf({
templateId: 'YOUR_TEMPLATE_ID',
variables: { customerName, invoiceNumber, items, total },
});
// Wait for the document to be ready
const completed = await rynko.documents.waitForCompletion(job.jobId, {
timeout: 30000,
});
if (completed.status === 'failed') {
return res.status(500).json({ error: completed.errorMessage });
}
res.json({
downloadUrl: completed.downloadUrl,
jobId: completed.jobId,
});
} catch (error) {
console.error('Generation failed:', error.message);
res.status(500).json({ error: 'Document generation failed' });
}
});
const port = process.env.PORT || 4000;
app.listen(port, () => console.log(`Server running on port ${port}`));
tip
Replace YOUR_TEMPLATE_ID with the ID of your template from the Rynko dashboard.
Step 4: Test the Endpoint
Start the server and send a request:
node server.js
curl -X POST http://localhost:4000/api/invoices/pdf \
-H "Content-Type: application/json" \
-d '{
"customerName": "Acme Corp",
"invoiceNumber": "INV-001",
"items": [
{ "description": "Widget", "quantity": 5, "price": 20.00 }
],
"total": 100.00
}'
You should receive a response with a downloadUrl to your generated PDF.
Error Handling
Use the RynkoError class to handle API-specific errors:
import { RynkoError } from '@rynko/sdk';
app.post('/api/invoices/pdf', async (req, res) => {
try {
const job = await rynko.documents.generatePdf({ /* ... */ });
const completed = await rynko.documents.waitForCompletion(job.jobId);
res.json({ downloadUrl: completed.downloadUrl });
} catch (error) {
if (error instanceof RynkoError) {
if (error.code === 'ERR_QUOTA_001') {
return res.status(429).json({ error: 'Document quota exceeded' });
}
return res.status(error.statusCode).json({ error: error.message });
}
res.status(500).json({ error: 'Internal server error' });
}
});
Next Steps
- Batch generate documents for multiple records
- Set up webhooks for async notifications instead of polling
- Generate Excel reports from templates