A proxy server sits between your frontend and the core system's API: your frontend sends only non-sensitive context (like a user ID or an assistant type), and your proxy maps that to an actual assistant configuration, forwards the request to the core system's API using a server-side private API key, and returns the response. Here's a complete Cloudflare Workers implementation:
export default {
async fetch(request, env, ctx) {
const url = new URL(request.url);
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization'
};
if (request.method === 'OPTIONS') {
return new Response(null, { status: 204, headers: corsHeaders });
}
if (url.pathname.startsWith('/call')) {
try {
const { userId, assistantType, ...rest } = await request.json();
const assistantConfig = getAssistantConfig(userId, assistantType, rest);
const response = await fetch(`https://api.sulus.ai${url.pathname}`, {
method: 'POST',
headers: {
Authorization: `Bearer ${env.SULUS_API_KEY}`, // Read from environment variables
'Content-Type': 'application/json',
},
body: JSON.stringify(assistantConfig),
});
return new Response(response.body, {
status: response.status,
headers: { 'Content-Type': 'application/json', ...corsHeaders },
});
} catch (error) {
return new Response(
JSON.stringify({ error: 'Proxy error', details: String(error) }),
{ status: 500, headers: { 'Content-Type': 'application/json', ...corsHeaders } }
);
}
}
return new Response('Proxy running', { headers: corsHeaders });
},
};
function getAssistantConfig(userId, assistantType) {
if (assistantType === 'existing') {
return { assistantId: 'YOUR_ASSISTANT_ID' };
}
return {
assistant: {
// Your transient assistant config here
},
};
}
The same three steps apply regardless of which serverless platform you choose — extract the custom data your frontend sends, map it to an assistant configuration, and call the core system's API with your server-side key, returning the response to the client. This applies equally to Vercel serverless functions, Supabase Edge Functions, and other similar platforms.