Cloudflare Docs
Workers
Workers
Visit Workers on GitHub
Set theme to dark (⇧+D)

Return JSON

Return JSON directly from a Worker script, useful for building APIs and middleware.

export default {
async fetch(request) {
const data = {
hello: "world",
};
const json = JSON.stringify(data, null, 2);
return new Response(json, {
headers: {
"content-type": "application/json;charset=UTF-8",
},
});
},
};

const handler: ExportedHandler = {
async fetch(request: Request) {
const data = {
hello: "world",
};
const json = JSON.stringify(data, null, 2);
return new Response(json, {
headers: {
"content-type": "application/json;charset=UTF-8",
},
});
},
};