Worker uploaded via API: fetch vs service worker

I have been upoading workers via the api using this API: https://developers.cloudflare.com/api/operations/worker-script-upload-worker-module

There are 2 types of runtime APIs when using workers, I think they are referred to as "fetch" and "service worker". What I mean is that I can write my worker like this:

export default {
    async fetch(request, env, ctx) {
        return new Response('Hello World!');
    },
};


Or:
addEventListener("fetch", (event) => event.respondWith(new Response("hello")));


I think the first one is preferred (fetch). However, when I upload to the API, only the the other one works (service worker). I'm therefore assuming that the service worker API is what is actually being run, it is a lower level thing. The fetch functionality is added by wrangler when it builds. So when uploading via the API we have to use the service worker API. Which is totally ok, just want to check if this is the case?
Interact with Cloudflare's products and services via the Cloudflare API
Was this page helpful?