Skip to main content
Version: Next

Getting Started

Note: Petitio supports both require and import syntax out of the box, so there's no need to do any pesky ESM or CJS hacks.

Basic Usage#

The default export of Petitio is a wrapper function for PetitioRequest, meaning all you need to get started is as follows:

import petitio from "petitio";
const request = petitio("https://example.com");

That instantiates a PetitioRequest for example.com. Simple right? Let's take a look at how we send requests and work with responses.

const result = await request.send().text();
// equivalent to
const result = await request.text();

Fast and clean. Beautiful, even. But how does that work? PetitioRequest#send instructs Petitio to send the current PetitioRequest and returns a PetitioResponse with the response data, and then PetitioResponse#text parses the response body from a buffer to a string. The shorthand form of this is PetitioRequest#text, which sends the request and invokes text parsing for you in one call.

That's the basics covered! Check out some more usage examples, or read through our other guides.

Last updated on