Skip to main content

HTTP Requests

The native fetch API is available in every runtime environment, so you can hit any HTTP API without installing a client.

Sending a GET request

const response = await fetch('https://jsonplaceholder.typicode.com/posts/1')
await response.json()

GET request

Sending a POST request with JSON

const response = await fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
title: 'Hello',
body: 'From RunJS',
userId: 1,
}),
})

await response.json()

Sending headers and auth tokens

Pull tokens from environment variables so they aren't hard-coded:

const response = await fetch('https://api.example.com/me', {
headers: {
Authorization: `Bearer ${process.env.API_TOKEN}`,
},
})

await response.json()