Edit

Share via


Create agent identities in agent identity platform

After you create an agent identity blueprint, the next step is to create one or more agent identities that represent AI agents in your test tenant. Agent identity creation is typically performed when provisioning a new AI agent.

This article guides you through the process of building a simple web service that creates agent identities via Microsoft Graph APIs.

If you want to quickly create agent identities for testing purposes, consider using this PowerShell module for creating and using agent identities.

Prerequisites

Before creating agent identities, ensure you have:

  • Understand agent identities
  • A configured agent identity blueprint (see Create an agent blueprint). Record the agent identity blueprint app ID from the creation process
  • A web service or application (running locally or deployed to Azure) that host the agent identity creation logic

Get an access token using agent identity blueprint

You use the agent identity blueprint to create each agent identity. Request an access token from Microsoft Entra using your agent identity blueprint:

When using a managed identity as a credential, you must first obtain an access token using your managed identity. Managed identity tokens can be requested from an IP address locally exposed in the compute environment. Refer to the managed identity documentation for details.

GET http://169.254.169.254/metadata/identity/oauth2/token?api-version=2019-08-01&resource=api://AzureADTokenExchange/.default
Metadata: True

After you obtain a token for the managed identity, request a token for the agent identity blueprint:

POST https://login.microsoftonline.com/<my-test-tenant>/oauth2/v2.0/token
Content-Type: application/x-www-form-urlencoded

client_id=<agent-blueprint-id>
scope=https://graph.microsoft.com/.default
client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer
client_assertion=<msi-token>
grant_type=client_credentials

A client_secret parameter can also be used instead of client_assertion and client_assertion_type, when a client secret is being used in local development.

Create an agent identity

Using the access token acquired in the previous step, you can now create agent identities in your test tenant. Agent identity creation might occur in response to many different events or triggers, such as a user selecting a button to create a new agent.

We recommend you create one agent identity for each agent, but you might choose a different approach based on your needs.

Always include the OData-Version header when using @odata.type.

POST https://graph.microsoft.com/beta/serviceprincipals/Microsoft.Graph.AgentIdentity
OData-Version: 4.0
Content-Type: application/json
Authorization: Bearer <token>
{
    "displayName": "My Agent Identity",
    "agentIdentityBlueprintId": "<my-agent-blueprint-id>",
    "sponsors@odata.bind": [
        "https://graph.microsoft.com/v1.0/users/<id>",
        "https://graph.microsoft.com/v1.0/groups/<id>"
    ],
}

Delete an agent identity

When an agent is deallocated or destroyed, your service should also delete the associated agent identity:

DELETE https://graph.microsoft.com/beta/serviceprincipals/<agent-identity-id>
OData-Version: 4.0
Content-Type: application/json
Authorization: Bearer <token>