Skip to main content

One post tagged with "microservices"

View All Tags

· 2 min read
Josh Twist

We just published a new video showing how you can add smart routing, behind a single common API for multiple backends, in 1 page of TypeScript. Metadata is loaded from an external service (in this case, Xata but you could use Supabase, Mongo etc).

Here's the code used in the demonstration:

import {
} from "@zuplo/runtime";

interface RouteInfo {
customerId: string;
primaryUrl: string;
secondaryUrl?: string;


async function loadRouteInfoFromApi(context: ZuploContext) {
const cache = new ZoneCache(CACHE_NAME, context);

const records = await cache.get(CACHE_KEY);

if (!records) {
const options = {
method: "POST",
headers: {
Authorization: `Bearer ${environment.XATA_API_KEY}`,
"Content-Type": "application/json",
body: '{"page":{"size":15}}',

const response = await fetch(

const data = await response.json();
cache.put(CACHE_KEY, data.records, 300); // 5 minutes"RouteInfo loaded from API");
return data.records;
}"RouteInfo loaded from Cache");
return records;

export default async function (request: ZuploRequest, context: ZuploContext) {
const customerId =;

const routing = await loadRouteInfoFromApi(context);

const routeInfo = routing.find((r) => r.customerId === customerId);

if (!routeInfo) {
return new Response(`No route found for customer '${customerId}'`, {
status: 404,

const response = await fetch(routeInfo.primaryUrl);
if (response.status !== 200 && routeInfo.secondaryUrl) {
`First request failed, trying secondary (${response.status})`
const response2 = await fetch(routeInfo.secondaryUrl);
return response2;

return response;

Got questions or feedback? Join us on Discord.