Code Examples

Learn by Example

Real-world code examples to help you integrate DevForge into your applications quickly.

Basic Authentication

API key authentication flow

Beginner

Learn how to authenticate your requests using API keys with proper security practices.

import { DevForge } from '@devforge/sdk';

const client = new DevForge({
  apiKey: process.env.DEVFORGE_API_KEY,
  // Optional: configure timeout
  timeout: 30000,
});

// All requests are now authenticated
const user = await client.users.me();

Webhook Handler

Process incoming webhooks

Intermediate

Securely verify and process webhook payloads with signature verification.

import { verifyWebhook } from '@devforge/sdk';

app.post('/webhook', (req, res) => {
  const signature = req.headers['x-devforge-signature'];

  if (!verifyWebhook(req.body, signature)) {
    return res.status(401).send('Invalid');
  }

  // Process the webhook
  console.log(req.body.event, req.body.data);
});

Real-time Data Sync

Subscribe to live updates

Intermediate

Use WebSocket subscriptions to receive real-time updates from your projects.

const subscription = client.subscribe({
  project: 'proj_abc123',
  events: ['data.created', 'data.updated'],
});

subscription.on('data', (event) => {
  console.log('Received:', event.type);
  // Update your UI in real-time
  updateUI(event.payload);
});

subscription.on('error', (err) => {
  console.error('Connection error:', err);
});

Edge Function

Deploy to 200+ locations

Advanced

Create and deploy serverless functions that run at the edge for minimal latency.

// edge-function.js
export default {
  async fetch(request, env) {
    const url = new URL(request.url);

    // Cache at the edge for 1 hour
    const cache = caches.default;
    let response = await cache.match(request);

    if (!response) {
      response = await processRequest(request);
      ctx.waitUntil(cache.put(request, response));
    }
    return response;
  }
};

Batch Operations

Process items in bulk

Intermediate

Efficiently process thousands of records with batch API operations.

const items = [
  { id: '1', data: { name: 'Item 1' } },
  { id: '2', data: { name: 'Item 2' } },
  // ... up to 1000 items per batch
];

const results = await client.batch({
  operations: items.map(item => ({
    method: 'upsert',
    resource: 'records',
    ...item
  }))
});

console.log(`Processed ${results.success} items`);

Error Handling

Handle errors gracefully

Beginner

Best practices for handling API errors and implementing retry logic.

import { DevForgeError } from '@devforge/sdk';

try {
  const result = await client.projects.create({...});
} catch (error) {
  if (error instanceof DevForgeError) {
    switch (error.code) {
      case 'rate_limited':
        await sleep(error.retryAfter);
        break;
      case 'invalid_request':
        console.error(error.details);
        break;
    }
  }
}

More Examples on GitHub

Explore our complete collection of examples, templates, and starter projects on GitHub.

View on GitHub