Skip to main content

Integration Guide

This guide provides code examples for creating jobs and handling webhooks in popular programming languages. Each example demonstrates how to:
  1. Create a job via the QuedUp API.
  2. Handle the webhook in your application.

Node.js (Express)

1. Setup Webhook Handler

const express = require('express');
const app = express();
const port = 3000;

app.use(express.json());

// Your webhook endpoint
app.post('/my-webhook', (req, res) => {
  // Process the job
  const data = req.body;
  console.log('Received job:', data);

  // ... perform your background task here ...

  // Respond to QuedUp
  res.status(200).send('Job received');
});

app.listen(port, () => {
  console.log(`Server running on port ${port}`);
});

2. Create Job

const fetch = require('node-fetch'); // or built-in fetch in Node 18+

const createJob = async () => {
  const response = await fetch('https://api.quedup.dev/jobs', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer YOUR_QUEDUP_API_KEY',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      name: 'Node.js Background Task',
      url: 'https://your-app.com/my-webhook',
      method: 'POST',
      run_at: new Date(Date.now() + 60000).toISOString(), // Run in 1 minute
      body: JSON.stringify({ task_id: 123, action: 'process_data' })
    })
  });

  const data = await response.json();
  console.log('Job created:', data);
};

createJob();

Python (Flask)

1. Setup Webhook Handler

from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/my-webhook', methods=['POST'])
def webhook():
    # Process the job
    data = request.json
    print(f"Received job: {data}")

    # ... perform your background task here ...

    # Respond to QuedUp
    return 'Job received', 200

if __name__ == '__main__':
    app.run(port=3000)

2. Create Job

import requests
import datetime
import json

url = "https://api.quedup.dev/jobs"
run_at = (datetime.datetime.utcnow() + datetime.timedelta(minutes=1)).isoformat() + "Z"

payload = {
    "name": "Python Background Task",
    "url": "https://your-app.com/my-webhook",
    "method": "POST",
    "run_at": run_at,
    "body": json.dumps({"task_id": 123, "action": "process_data"})
}

headers = {
    "Authorization": "Bearer YOUR_QUEDUP_API_KEY",
    "Content-Type": "application/json"
}

response = requests.post(url, json=payload, headers=headers)
print(response.json())

Go (net/http)

1. Setup Webhook Handler

package main

import (
	"encoding/json"
	"fmt"
	"io"
	"net/http"
)

func webhookHandler(w http.ResponseWriter, r *http.Request) {
	// Process the job
	body, _ := io.ReadAll(r.Body)
	fmt.Printf("Received job: %s\n", string(body))

	// ... perform your background task here ...

	// Respond to QuedUp
	w.WriteHeader(http.StatusOK)
	w.Write([]byte("Job received"))
}

func main() {
	http.HandleFunc("/my-webhook", webhookHandler)
	fmt.Println("Server running on port 3000")
	http.ListenAndServe(":3000", nil)
}

2. Create Job

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
	"time"
)

func main() {
	// Prepare payload
	runAt := time.Now().UTC().Add(1 * time.Minute).Format(time.RFC3339)
	
	requestBody, _ := json.Marshal(map[string]interface{}{
		"task_id": 123,
		"action":  "process_data",
	})

	jobPayload := map[string]interface{}{
		"name":   "Go Background Task",
		"url":    "https://your-app.com/my-webhook",
		"method": "POST",
		"run_at": runAt,
		"body":   string(requestBody),
	}

	jsonPayload, _ := json.Marshal(jobPayload)

	// Create Request
	req, _ := http.NewRequest("POST", "https://api.quedup.dev/jobs", bytes.NewBuffer(jsonPayload))
	req.Header.Set("Authorization", "Bearer YOUR_QUEDUP_API_KEY")
	req.Header.Set("Content-Type", "application/json")

	// Send Request
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	fmt.Println("Job Status:", resp.Status)
}