azure-sdk-for-js/sdk/maps/maps-geolocation-rest
Lazar 7d256c78ff
Update release dates for Geolocation, Render, and Search (#31779)
2024-11-15 03:37:16 +00:00
..
generated Update Geolocation SDK for metadata accuracy, v2 alignment, and latest TypeScript SDK compatibility (#31560) 2024-11-12 09:31:18 -08:00
review Update Geolocation SDK for metadata accuracy, v2 alignment, and latest TypeScript SDK compatibility (#31560) 2024-11-12 09:31:18 -08:00
samples/v1-beta [EngSys] move to vendored version of cross-env via dev-tool 2024-11-02 00:48:06 +00:00
samples-dev [EngSys] standardize OSS copyright header 2024-08-27 13:01:38 -07:00
src Update Geolocation SDK for metadata accuracy, v2 alignment, and latest TypeScript SDK compatibility (#31560) 2024-11-12 09:31:18 -08:00
swagger Update Geolocation SDK for metadata accuracy, v2 alignment, and latest TypeScript SDK compatibility (#31560) 2024-11-12 09:31:18 -08:00
test/public [eslint-plugin] add rule "@typescript-eslint/consistent-type-imports": "warn" 2024-10-30 15:48:52 +00:00
CHANGELOG.md Update release dates for Geolocation, Render, and Search (#31779) 2024-11-15 03:37:16 +00:00
LICENSE [Maps] Onboarding Maps Geolocation (#23956) 2022-12-02 15:21:17 +08:00
README.md [Maps] Migrate test to federated authentication (#30420) 2024-08-07 13:47:02 +08:00
api-extractor.json [Maps] Onboarding Maps Geolocation (#23956) 2022-12-02 15:21:17 +08:00
assets.json [Maps] Migrate test to federated authentication (#30420) 2024-08-07 13:47:02 +08:00
eslint.config.mjs Apply automated migration to eslint flat config 2024-08-22 11:43:19 -07:00
karma.conf.js [EngSys] standardize OSS copyright header 2024-08-27 13:01:38 -07:00
package.json Update Geolocation SDK for metadata accuracy, v2 alignment, and latest TypeScript SDK compatibility (#31560) 2024-11-12 09:31:18 -08:00
sample.env [Maps] Migrate test to federated authentication (#30420) 2024-08-07 13:47:02 +08:00
tests.yml Switch to TME test subscription (#31329) 2024-10-14 18:19:12 -07:00
tsconfig.json [EngSys] remove tsconfig.package.json 2024-07-16 13:27:25 +00:00

README.md

Azure Maps Geolocation REST client library for JavaScript

Azure Maps Geolocation Client

**If you are not familiar with our REST client, please spend 5 minutes to take a look at our REST client docs to use this library, the REST client provides a light-weighted & developer friendly way to call azure rest api

Key links:

Getting started

Currently supported environments

Prerequisites

If you use Azure CLI, replace <resource-group-name> and <map-account-name> of your choice, and select a proper pricing tier based on your needs via the <sku-name> parameter. Please refer to Azure Maps Reference for Azure CLI for more details.

az maps account create --resource-group <resource-group-name> --name <map-account-name> --sku <sku-name>

Install the @azure-rest/maps-geolocation package

Install the Azure Maps Geolocation REST client REST client library for JavaScript with npm:

npm install @azure-rest/maps-geolocation

Create and authenticate a MapsGeolocationClient

You'll need a credential instance for authentication when creating the MapsGeolocationClient instance used to access the Azure Maps render APIs. You can use either a Microsoft Entra ID credential or an Azure subscription key to authenticate. For more information on authentication, see Authentication with Azure Maps.

Using an Microsoft Entra ID credential

To use an Microsoft Entra ID token credential, provide an instance of the desired credential type obtained from the @azure/identity library.

To authenticate with Microsoft Entra ID, you must first npm install @azure/identity

After setup, you can choose which type of credential from @azure/identity to use. As an example, DefaultAzureCredential can be used to authenticate the client.

You'll need to register the new Microsoft Entra ID application and grant access to Azure Maps by assigning the required role to your service principal. For more information, see Host a daemon on non-Azure resources. Set the values of the client ID, tenant ID, and client secret of the Microsoft Entra ID application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET.

You will also need to specify the Azure Maps resource you intend to use by specifying the clientId in the client options. The Azure Maps resource client id can be found in the Authentication sections in the Azure Maps resource. Please refer to the documentation on how to find it.

const MapsGeolocation = require("@azure-rest/maps-geolocation").default;
const { DefaultAzureCredential } = require("@azure/identity");
const credential = new DefaultAzureCredential();
const client = MapsGeolocation(credential, "<maps-account-client-id>");

Using a Subscription Key Credential

You can authenticate with your Azure Maps Subscription Key. Please install the"@azure/core-auth"package:

npm install @azure/core-auth
const MapsGeolocation = require("@azure-rest/maps-geolocation").default;
const { AzureKeyCredential } = require("@azure/core-auth");
const credential = new AzureKeyCredential("<subscription-key>");
const client = MapsGeolocation(credential);

Using a Shared Access Signature (SAS) Token Credential

Shared access signature (SAS) tokens are authentication tokens created using the JSON Web token (JWT) format and are cryptographically signed to prove authentication for an application to the Azure Maps REST API.

You can get the SAS token using AzureMapsManagementClient.accounts.listSas from "@azure/arm-maps" package. Please follow the section Create and authenticate a AzureMapsManagementClient to setup first.

Second, follow Managed identities for Azure Maps to create a managed identity for your Azure Maps account. Copy the principal ID (object ID) of the managed identity.

Third, you will need to install"@azure/core-auth"package to use AzureSASCredential:

npm install @azure/core-auth

Finally, you can use the SAS token to authenticate the client:

const MapsGeolocation = require("@azure-rest/maps-geolocation").default;
const { AzureSASCredential } = require("@azure/core-auth");
const { DefaultAzureCredential } = require("@azure/identity");
const { AzureMapsManagementClient } = require("@azure/arm-maps");

const subscriptionId = "<subscription ID of the map account>";
const resourceGroupName = "<resource group name of the map account>";
const accountName = "<name of the map account>";
const mapsAccountSasParameters = {
  start: "<start time in ISO format>", // e.g. "2023-11-24T03:51:53.161Z"
  expiry: "<expiry time in ISO format>", // maximum value to start + 1 day
  maxRatePerSecond: 500,
  principalId: "<principle ID (object ID) of the managed identity>",
  signingKey: "primaryKey",
};
const credential = new DefaultAzureCredential();
const managementClient = new AzureMapsManagementClient(credential, subscriptionId);
const { accountSasToken } = await managementClient.accounts.listSas(
  resourceGroupName,
  accountName,
  mapsAccountSasParameters,
);
if (accountSasToken === undefined) {
  throw new Error("No accountSasToken was found for the Maps Account.");
}
const sasCredential = new AzureSASCredential(accountSasToken);
const client = MapsGeolocation(sasCredential);

Key concepts

MapsGeolocationClient

MapsGeolocationClient is the primary interface for developers using the Azure Maps Geolocation client library. Explore the methods on this client object to understand the different features of the Azure Maps Geolocation service that you can access.

Examples

You can get the country code from a IP address:

const { isUnexpected } = require("@azure-rest/maps-geolocation");

const result = await client.path("/geolocation/ip/{format}", "json").get({
  queryParameters: { ip: "2001:4898:80e8:b::189" },
});

if (isUnexpected(result)) {
  throw result.body.error;
}
if (!result.body.countryRegion) {
  throw new Error("No country region was found for the IP address.");
}
console.log(`The country code for the IP address is ${result.body.countryRegion.isoCode}`);

Troubleshooting

Logging

Enabling logging may help uncover useful information about failures. In order to see a log of HTTP requests and responses, set the AZURE_LOG_LEVEL environment variable to info. Alternatively, logging can be enabled at runtime by calling setLogLevel in the @azure/logger:

const { setLogLevel } = require("@azure/logger");

setLogLevel("info");

For more detailed instructions on how to enable logs, you can look at the @azure/logger package docs.

Impressions