SDKs · React & Next.js
Step 8 — Server calls & errors
Call the API from the server, and handle failures.
Call the API from the server
For search, reverse geocoding or routing in route handlers or server components, use the client directly with your key (no proxy):
import { UbHubGeo, NotFoundError } from "@ubhub/maps";
// Server only: route handlers, server actions, server components.
const ubhub = new UbHubGeo({ apiKey: process.env.UBHUB_API_KEY! });
const { results } = await ubhub.geocoding.search("Sukhbaatar Square", { limit: 5 });
const nearest = await ubhub.geocoding.reverse({ latitude: 47.9184, longitude: 106.9177 });
const { route } = await ubhub.routing.route({
origin: { latitude: 47.9184, longitude: 106.9177 },
destination: { latitude: 47.922, longitude: 106.9057 },
mode: "driving",
});In the browser, use new UbHubGeo({ proxyUrl: "/api/ubhub" }) or the useUbHub() hook. See Geocoding and Routing.
Handle errors
import { RateLimitError, UbHubError } from "@ubhub/maps";
try {
await ubhub.geocoding.search(query);
} catch (error) {
if (error instanceof RateLimitError) {
// No automatic retry. Wait error.retryAfterMs, then try again.
} else if (error instanceof UbHubError) {
// error.status, error.code, error.requestId (quote it to support)
} else {
throw error; // TypeError / RangeError: bad input, nothing was sent
}
}Every failure is a UbHubError with status, code and requestId. The SDK never retries; respect retryAfterMs. See Errors.
Deploy note
MapLibre v6's tile worker is served by the route from your installed
maplibre-gl. With a CSP, allow worker-src 'self' blob:. With output: "standalone", trace maplibre-gl-worker.mjs.