Back to Blog

Reduce Friction and Latency for Long-Running Jobs with Webhooks in Gemini API

May 9, 2026by Ichiban Team
geminiapiwebhooksaibackendarchitecture

Hero

#The New Way: Webhooks

Webhooks के साथ, initiation स्टेप पूरी तरह से retrieval स्टेप से अलग हो जाता है। आप बस request फायर करते हैं और अपना callback URL प्रोवाइड करते हैं:

// Modern Webhook Approach: Initiating the job
async function initiateVideoProcessing(fileId) {
  await fetch('https://api.gemini.com/v1/jobs/analyze', {
    method: 'POST',
    body: JSON.stringify({ 
      fileId: fileId, 
      prompt: "Extract key moments.",
      webhook_url: "https://api.ichiban-tools.com/webhooks/gemini/video-complete"
    })
  });
  // Return immediately; the user can continue navigating the app.
}

फिर आपका सर्वर रिजल्ट प्राप्त करने के लिए एक डेडिकेटेड endpoint एक्सपोज़ करता है जब भी वह तैयार हो:

// Express.js Webhook Receiver
app.post('/webhooks/gemini/video-complete', express.json(), (req, res) => {
  // 1. Verify the signature to ensure the request actually came from Google
  if (!verifyGeminiSignature(req)) {
    return res.status(401).send("Unauthorized");
  }

  const { jobId, status, result, error } = req.body;
  
  if (status === "COMPLETED") {
    // 2. Process the results asynchronously (e.g., save to DB, notify user via WebSocket)
    database.saveAnalysis(jobId, result);
    notifier.sendWebSocketMessage(jobId, "Analysis Complete!");
  } else if (status === "FAILED") {
    errorHandler.logAndAlert(jobId, error);
  }

  // 3. Acknowledge receipt quickly to prevent webhook retries
  res.status(200).send("OK");
});