The Problem: Opaque Failures in Edge-to-API Gateways
![]() |
| Image generated with Gemini AI |
When communicating with external marketing platforms like Brevo's API v3 through a serverless micro-proxy, relying solely on a binary response.ok check produces a significant architectural blind spot. Upstream endpoints issue distinct, structured HTTP status codes: 400 Bad Request for malformed addresses, 409 Conflict for duplicate contacts, and 429 Too Many Requests when rate quotas are hit.
The Solution: Granular Status Inspection and Structured Relaying
The Cloudflare Worker must act as a resilient gateway, safely inspecting the upstream response body, identifying specific machine-readable error codes, and returning actionable feedback payloads to the client-side JavaScript layer.
Resilient Gateway Implementation
export default {
async fetch(request, env) {
if (request.method === "OPTIONS") {
return new Response(null, {
headers: {
"Access-Control-Allow-Origin": "https://itchronicle.com",
"Access-Control-Allow-Methods": "POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type"
}
});
}
if (request.method !== "POST") {
return new Response(JSON.stringify({ error: "Method not allowed" }), { status: 405 });
}
try {
const { email, lang, redirectUrl } = await request.json();
const templateId = lang === "en" ? 12 : 11;
const listId = lang === "en" ? 5 : 4;
const brevoPayload = {
email: email,
includeListIds: [listId],
templateId: templateId,
redirectionUrl: redirectUrl
};
const brevoResponse = await fetch("https://api.brevo.com/v3/contacts/doubleOptinConfirmation", {
method: "POST",
headers: {
"api-key": env.BREVO_API_KEY,
"Content-Type": "application/json",
"Accept": "application/json"
},
body: JSON.stringify(brevoPayload)
});
// Safe JSON parsing with fallback
let data = {};
const responseText = await brevoResponse.text();
try {
data = JSON.parse(responseText);
} catch (e) {
data = { message: responseText };
}
if (!brevoResponse.ok) {
return new Response(JSON.stringify({
success: false,
status: brevoResponse.status,
code: data.code || "UPSTREAM_ERROR",
message: data.message || "Upstream provider error"
}), {
status: brevoResponse.status,
headers: {
"Access-Control-Allow-Origin": "https://itchronicle.com",
"Content-Type": "application/json"
}
});
}
return new Response(JSON.stringify({ success: true }), {
status: 200,
headers: {
"Access-Control-Allow-Origin": "https://itchronicle.com",
"Content-Type": "application/json"
}
});
} catch (err) {
return new Response(JSON.stringify({ success: false, message: err.message }), {
status: 500,
headers: {
"Access-Control-Allow-Origin": "https://itchronicle.com",
"Content-Type": "application/json"
}
});
}
}
};
Index: The Genesis of a Newsletter
- Ep. 1: Serverless Architecture with Cloudflare Workers and Brevo
- Ep. 2: Git Subtree and Delivery Decoupling
- Ep. 3: Cloudflare Workers, Anti-Spam Protection & Rate Limiting at the Edge
- Ep. 4: Advanced Brevo API Error Handling on Cloudflare Workers [You are here]
Your Reward: Brevo API Integration & Error Toolkit
Download our production-grade integration toolkit featuring granular status code mappers, structured retry patterns, and client-side error handlers. Enter your email to receive it.
Zero spam. Pure sysadmin architecture, operational code, and engineering field logs.





