Skip to main content

Avoid and favor sets

Avoid and favor set management is provided by @trimblemaps/services-react-native. Install it with the account package. See Getting started — Packages for pinned package versions.

Ensure the account is initialized before calling avoid/favor APIs.

List avoid/favor sets

import {
  getAvoidFavorSets,
  type AvoidFavorsOptions,
  type AvoidFavorsResult,
} from "@trimblemaps/services-react-native";

const result: AvoidFavorsResult = await getAvoidFavorSets({
  pageSize: 10,
  pageNumber: 1,
});

const count = result.afSets?.length ?? 0;
const total = result.totalAFSetCount ?? count;
const labels = result.afSets?.map(
  (set) => set.name ?? `Set ${set.setId}`,
);

The afSets array contains AvoidFavorSet objects. Each set exposes setId (number), not id. An empty account may return HTTP 204 or an empty body; handle that case in your UI:

function extractErrorMessage(error: unknown): string {
  if (error instanceof Error) {
    return error.message;
  }
  if (typeof error === "string") {
    return error;
  }
  if (error && typeof error === "object" && "message" in error) {
    const message = (error as { message?: unknown }).message;
    if (typeof message === "string") {
      return message;
    }
  }
  return "";
}

function isEmptyAvoidFavorsResponse(error: unknown): boolean {
  const message = extractErrorMessage(error).toLowerCase();
  if (!message) {
    return false;
  }

  return (
    message.includes("http 204") ||
    message.includes("empty avoid/favor") ||
    message.includes("no content") ||
    message.includes("unexpectedly found 0 bytes")
  );
}

try {
  return await getAvoidFavorSets({ pageSize: 10, pageNumber: 1 });
} catch (error) {
  if (isEmptyAvoidFavorsResponse(error)) {
    return { totalAFSetCount: 0, afSets: [] };
  }
  throw error;
}

Avoid/favor sets are account-level configuration. Use them with routing and directions APIs when building routes that respect your organization’s road preferences.

Last updated September 3, 2026.
Contents