Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
When using APIs, you might need access to Microsoft Defender for Endpoint without a user. For example, you might want to create a service that runs in the background and interacts with Defender for Endpoint on behalf of your organization. In this case, you need to create an application that can access Defender for Endpoint without a user.
API access requires OAuth2.0 authentication.
Prerequisites
Having the Microsoft Entra role for creating an app in Azure. For example, Application Administrator.
Step 1: Create an app in Azure
Sign in to the Azure portal.
Search for App registrations and navigate to App registrations.
Select New registration.
Choose a name for your application, and then select Register.
In your application page, go to Manage > Api Permissions > Add permission > APIs my organization uses.
In the Request API permissions page, search for WindowsDefenderATP and select it.
Select the type of permissions you require, and then select Add permissions.
Delegated permissions - Sign in with your app as if you were a user.
Application permissions - Access the API as a service.
Select the appropriate permissions for your app. To determine which permission you need, look at the Permissions section in the API you're calling. Here are two examples:
To run advanced queries, select Run advanced queries.
To isolate a device, select Isolate machine.
Select Add permission.
Step 2: Add a secret to your app
This section describes authenticating your app using an app secret. To authenticate your app using a certificate, see Create a self-signed public certificate to authenticate your application.
From the application page, select Certificates & secrets > New client secret.
In the Add a client secret pane, add a description and expiration date.
Select Add.
Copy the Value of the secret you created. You won't be able to retrieve this value after you leave the page.
From your app's overview page, copy the Application (client) ID and Directory (tenant) ID. You need this ID to authenticate your app.
Write down your application ID and your tenant ID. On your application page, go to Overview and copy the following.
Multitenant apps
Microsoft Defender for Endpoint partners need to set their apps to be multi-tenanted. Set your app to be a multitenant app if you're planning to create an app that will run in multiple customers' tenants.
In your Azure app page, go to Manage > Authentication.
Add a platform.
From the Configure platforms pane, select Web.
Add
https://portal.azure.comto Redirect URIs and select Configure.From the Supported account types options, select Accounts in any organizational directory and select Save.
Once you run your app, you need it to be approved in each tenant where you intend to use it. This is because your application interacts with Defender for Endpoint on behalf of your customer. You or your customer, will need to select the consent link and approve your app. Give consent with a user who has admin privileges.
Here's how to form the consent link. Replace 00000000-0000-0000-0000-000000000000 with your app ID.
https://login.microsoftonline.com/common/oauth2/authorize?prompt=consent&client_id=00000000-0000-0000-0000-000000000000&response_type=code&sso_reload=true
Get an access token
This section lists a few methods for getting your app's access token.
# This script acquires the App Context Token and stores it in the variable $token for later use.
# Paste your Tenant ID, App ID, and App Secret (App key) into the quotes below.
$tenantId = '' ### Paste your tenant ID here
$appId = '' ### Paste your Application ID here
$appSecret = '' ### Paste your Application key here
$sourceAppIdUri = 'https://api.securitycenter.microsoft.com/.default'
$oAuthUri = "https://login.microsoftonline.com/$TenantId/oauth2/v2.0/token"
$authBody = [Ordered] @{
scope = "$sourceAppIdUri"
client_id = "$appId"
client_secret = "$appSecret"
grant_type = 'client_credentials'
}
$authResponse = Invoke-RestMethod -Method Post -Uri $oAuthUri -Body $authBody -ErrorAction Stop
$token = $authResponse.access_token
$token
Validate the token
Follow the process in this section to ensure that you got the correct token. You can send more than one request with the same token. The token expires in an hour.
Copy and paste your token into JWT to decode it.
Validate that you get a roles claim with the desired permissions.
Use the token to access Microsoft Defender for Endpoint API
Choose the API you want to use.
Set the authorization header in the
httprequest you send toBearer {token}. Bearer is the authorization scheme.
Example
This example sends a request to get a list of alerts using C#.
var httpClient = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Get, "https://api.securitycenter.microsoft.com/api/alerts");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
var response = httpClient.SendAsync(request).GetAwaiter().GetResult();