Getting Started

Getting Started

Installation

To install CallApi, you have a couple of options:

Using npm (recommended)

First, run this command to install:

npm install @zayne-labs/callapi

Once that's done, you can import it into your JavaScript file like this:

app.js
import { callApi } from "@zayne-labs/callapi";

Without npm

If you prefer not to use npm, you can import CallApi directly into your JavaScript files via a CDN.

To do this, you first need to set your script's type to module, then import callApi.

<script type="module">
	import { callApi } from "https://esm.run/@zayne-labs/callapi";
</script>

You might also want to lock the version you're using. You can do that like this:

<script type="module">
	import { callApi } from "https://esm.run/@zayne-labs/callapi@0.3.2";
</script>

Usage

Wondering how to start using CallApi? It's simple!

You can use callApi just like the native fetch function. The cool part is that you don't have to handle the response yourself. You can just destructure the data and error directly.

  • All the options for the native fetch are supported, so you can use the same syntax to make requests.
const { data, error } = await callApi("url", fetchOptions);
  • You also have access to the response object itself via destructuring:
const { data, error, response } = await callApi("url", fetchOptions);

For additional convenience with autocomplete, check out how to use CallApi with TypeScript in the TypeScript section.