Cloudflare Docs
Workers
Edit this page
Report an issue with this page
Log into the Cloudflare dashboard
Set theme to dark (⇧+D)

Return JSON

Return JSON directly from a Worker script, useful for building APIs and middleware.
 Run Worker
export default {
async fetch(request) {
const data = {
hello: "world",
};
return Response.json(data);
},
};
export default {
async fetch(request): Promise<Response> {
const data = {
hello: "world",
};
return Response.json(data);
},
} satisfies ExportedHandler;
from js import Response, Headers
import json
def on_fetch(request):
data = json.dumps({"hello": "world"})
headers = Headers.new({"content-type": "application/json"}.items())
return Response.new(data, headers=headers)