7d256c78ff | ||
---|---|---|
.. | ||
generated | ||
review | ||
samples/v1-beta | ||
samples-dev | ||
src | ||
swagger | ||
test/public | ||
CHANGELOG.md | ||
LICENSE | ||
README.md | ||
api-extractor.json | ||
assets.json | ||
eslint.config.mjs | ||
karma.conf.js | ||
package.json | ||
sample.env | ||
tests.yml | ||
tsconfig.json |
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
- LTS versions of Node.js
- Latest versions of Safari, Chrome, Edge and Firefox.
Prerequisites
- You must have an Azure subscription to use this package.
- An Azure Maps account. You can create the resource via the Azure Portal, the Azure PowerShell, or the Azure CLI.
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.