Convert curl to TypeScript

curl2code converts curl commands to TypeScript code with proper type annotations using the fetch API. The conversion runs in your browser via WebAssembly — your data never leaves your device. For plain JavaScript, check our curl to JavaScript converter. For server-side code, see curl to Node.js. Below are typed examples for common scenarios.

How to copy curl from your browser

  1. 1

    Open DevTools

    Press F12 or Ctrl+Shift+I to open your browser developer tools.

  2. 2

    Go to the Network tab

    Click the Network tab and perform the action that triggers the HTTP request.

  3. 3

    Copy as cURL

    Right-click the request → CopyCopy as cURL. Then paste it above.

Frequently Asked Questions

What does curl2code generate for TypeScript?

curl2code generates TypeScript code using the browser-native fetch API with full type annotations. The generated code includes typed request options, proper Response handling, and type-safe JSON parsing. TypeScript adds compile-time safety to the same Fetch API used in plain JavaScript.

TypeScript fetch vs JavaScript fetch — what's the difference?

The runtime API is identical — TypeScript adds static type checking. You get typed Headers, RequestInit, and Response objects. For custom response shapes, define an interface and cast with await response.json() as MyType. For server-side TypeScript, check our curl to Node.js converter.

How to handle authentication in TypeScript?

Same as Fetch — pass an Authorization header in the typed HeadersInit object. TypeScript ensures you don't misspell header names if using a typed headers helper. curl2code generates properly typed auth headers from -u and -H 'Authorization: ...' curl flags.

How to send form data with TypeScript?

Use the browser's FormData API — TypeScript has built-in type definitions for it. Call formData.append('file', file) with typed parameters. The compiler catches mismatched types at build time. curl2code converts -F flags to properly typed FormData calls.

How to handle errors type-safely in TypeScript?

Use try/catch with async/await. Type the catch clause error as unknown and narrow with instanceof. Check response.ok before parsing. Define typed error responses with interfaces for API-specific error formats. This gives you compile-time guarantees that all error paths are handled.

How to type API responses in TypeScript?

Define an interface for the expected response shape, then use const data: MyAPI = await response.json(). For validation at runtime, pair with zod or io-ts. This pattern ensures type safety across your codebase. For a similar typed experience in other languages, see curl to Go or curl to Rust.

How to set timeouts for fetch in TypeScript?

Use AbortController — TypeScript includes full type definitions for the Abort API. Create const controller = new AbortController(), pass signal to fetch, and abort with setTimeout. The AbortSignal.timeout(ms) shorthand is typed in modern TypeScript lib targets.

How to handle CORS with TypeScript?

CORS handling is identical to JavaScript — it's a browser security feature, not a TypeScript one. Set mode: 'cors' and credentials: 'include' as needed, with TypeScript ensuring correct RequestMode and RequestCredentials enum values. For server-side requests where CORS doesn't apply, see curl to Node.js.

How to send a POST request with JSON body in TypeScript?

Convert a curl POST command like curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' URL to TypeScript using curl2code. The generated code uses const response: Response = await fetch(url, { method: 'POST', body: JSON.stringify(data) }). curl2code automatically detects -d/--data flags with JSON content and sets the appropriate Content-Type header in the fetch code.

How to add Bearer token authorization in TypeScript?

Pass curl -H "Authorization: Bearer YOUR_TOKEN" URL to curl2code and it generates fetch code with the auth header: headers: { Authorization: `Bearer ${token}` }. curl2code detects Bearer tokens from both -H "Authorization: Bearer ..." and --oauth2-bearer flags. For Basic auth, use -u user:pass.

How to set Content-Type header in TypeScript?

curl2code converts curl -H "Content-Type: application/json" URL to fetch code with the proper header: headers: { 'Content-Type': 'application/json' } as HeadersInit. For -d data, curl defaults to application/x-www-form-urlencoded; for -F form uploads, it uses multipart/form-data. curl2code maps each to the correct fetch API.

Useful Links

curl Guides

Convert curl to Other Languages