fetch() Code Generator

Fill in an API request and get JavaScript fetch() code you can paste into a browser app, a Node 18+ script or a Next.js route. JSON bodies are wrapped in JSON.stringify, forms use URLSearchParams or FormData, and the code checks response.ok, which fetch does not do for you.

Query parameters
Headers
Authentication
Body
Output

Writing the code runs in your browser. What you type never leaves this device.

This page only writes the code. It never sends your request or sees your token.

Features

  • JSON bodies written as objects inside JSON.stringify, so they stay readable

  • URLSearchParams for urlencoded forms and FormData for uploads

  • Leaves Content-Type to the browser for FormData, so the boundary is set

  • Checks response.ok and throws on 4xx and 5xx

  • Optional async function wrapper with try and catch

  • Same request as cURL, Axios or HTTPie in one click

Where fetch() code fits

Front-end API calls

Call your backend or a third-party API from React, Vue or plain JavaScript without adding a library.

Node and serverless functions

Node 18 and later ship fetch built in, so the same code runs in API routes, Lambdas and edge functions.

Turning API docs into code

Docs often show only cURL. Enter the same details here and get fetch code instead of translating by hand.

fetch() details that trip people up

fetch does not throw on HTTP errors

fetch only rejects when the network fails. A 404 or 500 still resolves, so always check response.ok (true for status 200 to 299) before reading the body. The generated code does this and throws an error with the status.

Bodies and Content-Type

  • JSON: send JSON.stringify(data) with Content-Type: application/json.
  • Forms: new URLSearchParams({...}) sends application/x-www-form-urlencoded.
  • Files: new FormData() with append(). Do not set Content-Type yourself; the browser adds it with the boundary.
  • GET and HEAD requests cannot have a body; fetch throws if you pass one.

CORS in the browser

From a web page, the server must allow your origin with Access-Control-Allow-Origin, or the browser hides the response. The same code works in Node without CORS. If a request works in cURL but fails in the browser, CORS is the usual reason.

fetch() Code Generator - FAQ