Using Cloudflare Workers & Edge API (JavaScript & TypeScript)
Generate your TokenID from CubeMaster Online and securely attach it as an environment secret in Wrangler CLI:
wrangler secret put CUBEMASTER_TOKEN_ID
Create `src/index.js` in your Cloudflare Worker project to handle edge load requests:
export default {
async fetch(request, env, ctx) {
if (request.method !== 'POST') {
return new Response('Method Not Allowed', { status: 405 });
}
const incomingJson = await request.json();
const payload = {
"Title": incomingJson.title || "Edge Load Plan",
"Cargoes": incomingJson.cargoes,
"Containers": incomingJson.containers,
"Rules": incomingJson.rules || { "FillDirection": "FrontToRear", "CalculationType": "MixLoad" }
};
const response = await fetch('https://api.cubemaster.net/loads', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'TokenID': env.CUBEMASTER_TOKEN_ID
},
body: JSON.stringify(payload)
});
const result = await response.json();
return new Response(JSON.stringify(result), {
headers: { 'Content-Type': 'application/json' }
});
}
};
Deploy your worker globally with npx wrangler deploy. Use Cloudflare KV (`env.LOAD_CACHE.put()`) to cache calculated load diagrams for ultra-fast response times at 300+ edge data centers worldwide.