Skip to main content

Complete Implementation Examples

Below are full working examples of a Custom CRM API implementation in popular frameworks.
const express = require('express');
const app = express();

app.use(express.json());

// Middleware to validate API key
const validateApiKey = (req, res, next) => {
  const apiKey = req.headers['x-api-key'];
  if (apiKey !== process.env.API_KEY) {
    return res.status(401).json({ error: 'Invalid API key' });
  }
  next();
};

app.use(validateApiKey);

// Health endpoint
app.get('/health', (req, res) => {
  res.json({ status: 'ok' });
});

// Create task
app.post('/tasks', async (req, res) => {
  const { subject, description, type, ownerId, dueDate, contactId, companyId } = req.body;
  
  const task = await db.tasks.create({
    data: {
      subject,
      description,
      type,
      ownerId,
      dueDate: dueDate ? new Date(dueDate) : null,
      contactId,
      companyId,
      completed: false,
    },
  });
  
  res.status(201).json(task);
});

// Get task
app.get('/tasks/:taskId', async (req, res) => {
  const task = await db.tasks.findUnique({ where: { id: req.params.taskId } });
  
  if (!task) {
    return res.status(404).json({ error: 'Task not found' });
  }
  
  res.json(task);
});

// Get tasks batch
app.post('/tasks/batch', async (req, res) => {
  const { ids } = req.body;
  
  const tasks = await db.tasks.findMany({
    where: { id: { in: ids } },
  });
  
  res.json(tasks);
});

// Update task
app.patch('/tasks/:taskId', async (req, res) => {
  const { completed } = req.body;
  
  const task = await db.tasks.update({
    where: { id: req.params.taskId },
    data: {
      completed,
      completedAt: completed ? new Date() : null,
    },
  });
  
  res.json(task);
});

// Complete tasks batch
app.patch('/tasks/batch', async (req, res) => {
  const { ids, completed } = req.body;
  
  await db.tasks.updateMany({
    where: { id: { in: ids } },
    data: {
      completed,
      completedAt: completed ? new Date() : null,
    },
  });
  
  // Return updated tasks
  const updatedTasks = await db.tasks.findMany({
    where: { id: { in: ids } },
  });
  
  res.json(updatedTasks);
});

app.listen(3000);

Minimal Implementation

If you want to get started quickly, here’s a minimal implementation with just the essential endpoints:
Node.js (Minimal)
const express = require('express');
const { v4: uuidv4 } = require('uuid');

const app = express();
app.use(express.json());

const API_KEY = process.env.API_KEY;
const tasks = new Map();

// Auth middleware
app.use((req, res, next) => {
  if (req.headers['x-api-key'] !== API_KEY) {
    return res.status(401).json({ error: 'Unauthorized' });
  }
  next();
});

// Health
app.get('/health', (req, res) => res.json({ status: 'ok' }));

// Create task
app.post('/tasks', (req, res) => {
  const task = { id: uuidv4(), ...req.body, completed: false };
  tasks.set(task.id, task);
  res.status(201).json(task);
});

// Get task
app.get('/tasks/:id', (req, res) => {
  const task = tasks.get(req.params.id);
  if (!task) return res.status(404).json({ error: 'Not found' });
  res.json(task);
});

// Get tasks batch
app.post('/tasks/batch', (req, res) => {
  const result = req.body.ids
    .map(id => tasks.get(id))
    .filter(Boolean);
  res.json(result);
});

// Update task
app.patch('/tasks/:id', (req, res) => {
  const task = tasks.get(req.params.id);
  if (!task) return res.status(404).json({ error: 'Not found' });
  
  Object.assign(task, req.body);
  if (req.body.completed) task.completedAt = new Date().toISOString();
  res.json(task);
});

// Complete tasks batch
app.patch('/tasks/batch', (req, res) => {
  const { ids, completed } = req.body;
  const updated = ids.map(id => {
    const task = tasks.get(id);
    if (task) {
      task.completed = completed;
      task.completedAt = completed ? new Date().toISOString() : null;
    }
    return task;
  }).filter(Boolean);
  res.json(updated);
});

app.listen(3000, () => console.log('Server running on port 3000'));

Testing Checklist

Before connecting to Genesy, ensure all these tests pass:
curl -X GET https://your-api.com/health \
  -H "X-API-Key: your-secret-key"
Expected: 200 OK with any JSON response
curl -X POST https://your-api.com/tasks \
  -H "X-API-Key: your-secret-key" \
  -H "Content-Type: application/json" \
  -d '{"subject": "Test task"}'
Expected: 201 Created with task object containing id
curl -X GET https://your-api.com/tasks/TASK_ID \
  -H "X-API-Key: your-secret-key"
Expected: 200 OK with task object
curl -X POST https://your-api.com/tasks/batch \
  -H "X-API-Key: your-secret-key" \
  -H "Content-Type: application/json" \
  -d '{"ids": ["TASK_ID_1", "TASK_ID_2"]}'
Expected: 200 OK with array of tasks
curl -X PATCH https://your-api.com/tasks/TASK_ID \
  -H "X-API-Key: your-secret-key" \
  -H "Content-Type: application/json" \
  -d '{"completed": true}'
Expected: 200 OK with updated task
curl -X PATCH https://your-api.com/tasks/batch \
  -H "X-API-Key: your-secret-key" \
  -H "Content-Type: application/json" \
  -d '{"ids": ["TASK_ID_1", "TASK_ID_2"], "completed": true}'
Expected: 200 OK with array of updated tasks

Next Steps

Connect Your CRM

Once your implementation is complete, connect your CRM to Genesy