HTTP requests in Deno with fetch

Deno implements the web-standard Fetch API as globals: fetch(), Request, Response and Headers. Code you write against it also runs in browsers, Node.js 18+ and Bun, and you don’t need a library like Axios for ordinary HTTP calls. The one Deno-specific part is permissions: a script can only reach hosts you allow with --allow-net.
The pieces
Section titled “The pieces”fetch(input, init?)takes a URL string,URLorRequest, plus optional settings (method,headers,body,signal, …), and returns aPromise<Response>.Requestdescribes a request. Build one when you want to reuse or pass around a request object; otherwise pass the URL and options straight tofetch.Responseholds the status, headers and body. The body can be read once, as.json(),.text(),.bytes(),.arrayBuffer()or.blob(), or consumed as a stream through.body.Headersis a case-insensitive map withget,set,append,hasanddelete.
A GET request
Section titled “A GET request”const response = await fetch("https://jsonplaceholder.typicode.com/posts/1");if (!response.ok) { throw new Error(`Request failed: ${response.status} ${response.statusText}`);}const post = await response.json();console.log(post.title);deno run --allow-net=jsonplaceholder.typicode.com get.tsfetch only rejects on network failures (DNS errors, refused connections, aborts). A 404 or 500 still resolves to a Response, so check response.ok or response.status yourself.
A POST request with a JSON body
Section titled “A POST request with a JSON body”const response = await fetch("https://jsonplaceholder.typicode.com/posts", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ title: "New post", body: "Sent from Deno", userId: 1 }),});
console.log(response.status); // 201console.log(await response.json());// { title: "New post", body: "Sent from Deno", userId: 1, id: 101 }body also accepts a string, Uint8Array, Blob, FormData, URLSearchParams or a ReadableStream. For FormData and URLSearchParams, fetch sets the Content-Type header for you.
Timeouts and cancellation
Section titled “Timeouts and cancellation”fetch has no timeout of its own. Pass an AbortSignal; AbortSignal.timeout(ms) creates one that fires after a delay:
try { const response = await fetch("https://httpbin.org/delay/5", { signal: AbortSignal.timeout(2000), }); console.log(response.status);} catch (err) { if (err instanceof DOMException && err.name === "TimeoutError") { console.log("Request timed out"); } else { throw err; }}Use an AbortController instead if you want to cancel from your own code (for example, when the user navigates away or another request wins a race).
Streaming a large download to disk
Section titled “Streaming a large download to disk”response.body is a ReadableStream<Uint8Array>, and a Deno file has a writable stream, so you can pipe one into the other without holding the whole response in memory:
const response = await fetch("https://jsonplaceholder.typicode.com/photos");const file = await Deno.create("photos.json");await response.body!.pipeTo(file.writable);pipeTo closes the file when the stream ends. This one needs --allow-net and --allow-write=photos.json.
Deno-specific behaviour
Section titled “Deno-specific behaviour”- Local files. Deno’s
fetchacceptsfile:URLs, soawait fetch(new URL("./data.json", import.meta.url))reads a file next to your module. It needs--allow-read. - Proxies and certificates.
Deno.createHttpClient()creates a client with options such as a proxy, extra CA certificates or HTTP/2 settings. Pass it asfetch(url, { client })and callclient.close()when you’re done. Deno also respects theHTTP_PROXY,HTTPS_PROXYandNO_PROXYenvironment variables. - No CORS. CORS is a browser rule. Deno doesn’t apply it to outgoing requests, so a request that fails in the browser may work from Deno.
Further reading
Section titled “Further reading”© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)