Get started

Fetch data

Client basics

@mountaya/client is one typed client for the three APIs. It holds your credential, builds every request, and hands back typed data.

Construct a client

import { Mountaya } from "@mountaya/client";

const mountaya = new Mountaya({
  credential: { publishableKey: "pk_your_publishable_key", sessionToken },
});

const { data: itineraries } = await mountaya.studio.itineraries.getItineraries({
  query: { limit: 10 },
});

A publishable key alone reads published content on the Map Studio and Tile APIs. Add a session token for the Data API, your drafts, and every write: authentication shows how to get one.

In a browser, import from @mountaya/client/browser.

Call the three APIs

NamespaceAPIHolds
studioMap Studio APIItineraries, zones, collections, and their catalogs
adminMap Studio APIThe organization: settings, members, keys, usage, billing, sessions
agentMap Studio APIAI agent runs
dataData APIRouting, search, terrain, isochrones, matrices
tilesTile APIStyle specifications and the tile layers
const { data: itinerary } = await mountaya.studio.itineraries.getItinerary({
  itinerary_id: itineraryId,
});

const { routes } = await mountaya.data.directions({
  activity: "HIKING_AND_TRAIL",
  waypoints: [
    [6.8712, 45.9231],
    [6.9271, 45.9822],
  ],
});

A Map Studio or Tile API method resolves to the envelope, with the payload at data. A Data API method resolves to the operation's own field, here routes. Method names are the API reference's operation ids.

Ask for fewer fields

Every Data API method sends a full GraphQL selection. Pass select to ask for less: the return type narrows to the fields you name.

const { routes } = await mountaya.data.directions(
  { activity: "HIKING_AND_TRAIL", waypoints },
  { select: { routes: { distance: true, duration: true, summary: true } } },
);

signal and timeoutMs go in the same options bag.

In React

Put the client on MountayaProvider, inside your QueryClientProvider:

import type { ReactNode } from "react";
import { Mountaya } from "@mountaya/client/browser";
import { MountayaProvider } from "@mountaya/client-react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";

const queryClient = new QueryClient();
const mountaya = new Mountaya({
  credential: { publishableKey: "pk_your_publishable_key" },
});

export const App = ({ children }: { children: ReactNode }) => (
  <QueryClientProvider client={queryClient}>
    <MountayaProvider client={mountaya}>{children}</MountayaProvider>
  </QueryClientProvider>
);

Then read with the hooks:

import { isMountayaApiError } from "@mountaya/client";
import { useItinerary } from "@mountaya/client-react";

const Itinerary = ({ itineraryId }: { itineraryId: string }) => {
  const { data, error, isPending } = useItinerary(itineraryId);

  if (isPending) return <Spinner />;
  if (error) {
    const text = isMountayaApiError(error) ? error.errors[0]?.message : error.message;
    return <ErrorMessage text={text} />;
  }

  return <h1>{data.name}</h1>;
};

useItineraries, useCollections and useZones read a listing a page at a time, with fetchNextPage. useMountayaQuery reads any other operation by name.

Next steps

Last updated