Tutorials

Build your first app

Build a small Node.js app that lists your restaurants and their details, end to end.

In this tutorial you will build a command-line app that lists the restaurants your API key can access. Along the way you'll learn how to structure queries, use variables and paginate.

Note

You need Node.js 20+ and an API key. If you don't have a key yet, follow the Quickstart first.

Set up the project

bash
mkdir loco-restaurants && cd loco-restaurants
npm init -y

Write a tiny client

Create loco.mjs:

javascript
export async function loco(query, variables = {}) {
  const res = await fetch("https://api.thelocoapp.com/graphql", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${process.env.LOCO_API_KEY}`,
    },
    body: JSON.stringify({ query, variables }),
  });
  const body = await res.json();
  if (body.errors?.length) throw new Error(body.errors.map((e) => e.message).join("\n"));
  return body.data;
}

List every restaurant

Create index.mjs:

javascript
import { loco } from "./loco.mjs";

const QUERY = `query Restaurants($after: String) {
  restaurants(first: 50, after: $after) {
    edges { node { id } }
    pageInfo { hasNextPage endCursor }
  }
}`;

let after = null;
do {
  const { restaurants } = await loco(QUERY, { after });
  for (const { node } of restaurants.edges) console.log(node.id);
  after = restaurants.pageInfo.hasNextPage ? restaurants.pageInfo.endCursor : null;
} while (after);

Run it:

bash
LOCO_API_KEY=... node index.mjs

What you learned

  • The API is one endpoint; the query decides what you get back.
  • Lists are paginated with cursors — see Pagination.
  • Explore more fields on Restaurant and add them to your query.