Skip to main content

Call an API

Call an Enmovil API from a screen or a service with request, and handle the result the same way in React, Node, and Python.

Before you begin​

Make a call and handle the result​

  1. Call request with the URL, the method, and the options:

    const r = await request(`${BASE_URL}/api/v1/rfq/list`, "GET", { params: { page: 2 }, token });
  2. Check r.ok. If it's false, handle the error, for example by logging the user out when r.status is 401:

    if (!r.ok) {
    if (r.status === 401) return logout();
    return showError(r.message || "Something went wrong");
    }
  3. Use r.data:

    setRows(r.data);

A 4xx or 5xx response comes back as a result with ok: false, not as an exception. request throws only for a wrong call, such as an unknown method or a GET with a body. For every field of the result, see request().

Send data​

Pass the data as body. request sends it as JSON:

await request(`${BASE_URL}/api/v1/rfq/bid`, "POST", { body: { rfqId: 7, amount: 1200 }, token });
await request(`${BASE_URL}/api/v1/rfq/bid/7`, "PUT", { body: { amount: 1300 }, token });
await request(`${BASE_URL}/api/v1/rfq/bid/7`, "PATCH", { body: { status: "won" }, token });
await request(`${BASE_URL}/api/v1/rfq/bid/7`, "DELETE", { token });

GET requests retry twice after a network error, a timeout, a 429, or a 5xx. Other methods don't retry unless you set retries, so nothing is saved twice.

Upload a file​

Pass the files as files, and any other fields as body. request sends them together as multipart:

await request(`${BASE_URL}/api/v1/upload`, "POST", { body: { folder: "rfq" }, files: { file }, token });

In Python, a file is bytes, an open file, or a tuple:

request(BASE_URL + "/api/v1/upload", "POST", body={"folder": "rfq"},
files={"file": ("a.pdf", data, "application/pdf")}, token=token)

Download a file​

Set responseType to "blob":

const r = await request(`${BASE_URL}/api/v1/invoice.pdf`, "GET", { responseType: "blob", token });

r.data is a Blob in JavaScript and bytes in Python.

Test code that calls an API​

Pass a transport function to replace the HTTP layer, so your tests need no network or server. For details, see request().