How to use custom bearer token authentication in Azure AI Agent Service with OpenAPI Specified Tools

Jagan P 25 Reputation points
2025-05-16T18:29:18.4933333+00:00

We are working on AzureA AI Agents with OpenAPI tools in Azure AI foundry.

I have configured an Open API tool with the json schema and Authentication method as (connection)

The Open API json schema is linked to a private webApplication (hosted in Kubernetes - https://forms-tstnf-internal.eu.hrx.alight.com) and the endpoints accepts only bearer token

"securitySchemes": {
      "Bearer": {
        "type": "apiKey",
        "description": "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
        "name": "Authorization",
        "in": "header"
      }
    }

User's image

The open api agent is connected to the below connected resource.

User's image


I tried the agent in playground, but it is not working. Below is the error message

"tool_calls": [
          {
            "id": "call_ehoPT0t42vGgQiUKXBlnDzFS",
            "type": "openapi",
            "function": {
              "name": "FormPackage_GetAllPackages",
              "arguments": "{\"Status\":\"Open\"}",
              "output": "Error occured while trying to execute http request.",
              "error": "Aiohttp client error occurred",
              "curl": "curl -X GET 'https://forms-tstnf-internal.eu.hrx.alight.com/api/packages?Status=Open' -H 'Content-Type: application/json' -H 'Accept: */*' -H 'Authorization: REDACTED'"
            }
          }

I am not sure if the hardcoded bearer token is sent correctly in the request header

or if the private open api endpoint is not accessible from the azure agent.


I have attached the open API json schema and the Agent run steps logs.

Also, can you please let me know how to generate new bearer token for each request and add it to the request header before agent calls the open api endpoint.

Packages Agent runSteps.log

packages swagger.log

Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
3,602 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jerald Felix 1,630 Reputation points
    2025-05-17T00:57:49.3266667+00:00

    Dear Jagan,

    To resolve your issue with Azure AI Agent Service using OpenAPI tools and bearer token authentication, you’ll need to ensure two major things are handled properly:

    1. Network Accessibility from Azure AI Agent Service

    Azure AI Agent Service must be able to reach the internal Kubernetes service endpoint:

    https://forms-tstnf-internal.eu.hrx.alight.com

    Solution Options

    :

    • Use Azure VNet integration: Your agent must be deployed in a VNet-enabled environment (e.g., Azure Container Apps with VNet, or Azure Functions Premium Plan with VNet support).
    • Private Endpoint or Internal DNS resolution must be available from the AI Agent’s execution environment.
    • Consider exposing the API via Azure API Management (APIM) with VNet + Private Link to securely bridge public Azure AI services and internal Kubernetes-hosted APIs.

    2. Dynamic Bearer Token Injection in Azure AI Agent Tool

    The agent won’t automatically rotate or inject dynamic bearer tokens unless you use a proper Connection setup and secure Azure Managed Identity or custom connection logic.

    Since you mentioned:

    “Authentication method as (connection)”

    You should follow this pattern using “connection” as a security method, not hardcoding bearer tokens directly in OpenAPI schema.

    Steps to Use Dynamic Bearer Token with Connection Authentication

    A. Modify OpenAPI Security Scheme (keep this)

    "securitySchemes": {

      "BearerAuth": {

        "type": "apiKey",

        "name": "Authorization",

        "in": "header",

        "description": "JWT Bearer token. Format: Bearer {token}"

      }

    }

    B. Add Security Requirement

    "security": [

      {

        "BearerAuth": []

      }

    ]

    C. Use Azure AI Agent Service’s Connection Feature

    • In Azure AI Foundry, go to your tool → Connections tab
    • Select “Add Connection” → Choose type “API Key”
    • Set:
      • Header Name: Authorization
        • Header Value Template: Bearer {{token}}
          • Replace {{token}} with a key vault reference or a logic app URL that returns a token dynamically

    D. Token Generation Options

    If your API requires a short-lived JWT:

    1. Option 1: Use Azure Logic App or Azure Function to generate and return a fresh token.
    2. Option 2: Manually paste token in connection for now if automation is not in place.

    E. Testing the Connection

    In Azure AI Foundry Playground, test your agent with:

    • Network access working (step 1 solved)
    • Connection set with Authorization header using the live token

    If this fails, the curl error should show the actual root cause:

    • 403 → token issue
    • 404 → wrong endpoint
    • timeout or aiohttp client error → VNet/firewall issue

    Summary Checklist

    Task Status
    Task Status
    Private API reachable from Azure AI Foundry? Ensure via VNet, APIM, or Private Link
    OpenAPI securitySchemes defined correctly? Yes
    Connection used for Authorization: Bearer {{token}}? Must do
    Token rotation setup? Optional but ideal
    Agent tool configured with the right connection? Verify in Azure Portal

    Would you like me to provide:

    • A sample Logic App that generates a bearer token dynamically?
    • A diagram explaining how OpenAPI + Agent + connection works with VNet?

    Let me know! To resolve your issue with Azure AI Agent Service using OpenAPI tools and bearer token authentication, you’ll need to ensure two major things are handled properly:

    1. Network Accessibility from Azure AI Agent Service

    Azure AI Agent Service must be able to reach the internal Kubernetes service endpoint:

    https://forms-tstnf-internal.eu.hrx.alight.com

    Solution Options

    :

    • Use Azure VNet integration: Your agent must be deployed in a VNet-enabled environment (e.g., Azure Container Apps with VNet, or Azure Functions Premium Plan with VNet support).
    • Private Endpoint or Internal DNS resolution must be available from the AI Agent’s execution environment.
    • Consider exposing the API via Azure API Management (APIM) with VNet + Private Link to securely bridge public Azure AI services and internal Kubernetes-hosted APIs.

    2. Dynamic Bearer Token Injection in Azure AI Agent Tool

    The agent won’t automatically rotate or inject dynamic bearer tokens unless you use a proper Connection setup and secure Azure Managed Identity or custom connection logic.

    Since you mentioned:

    “Authentication method as (connection)”

    You should follow this pattern using “connection” as a security method, not hardcoding bearer tokens directly in OpenAPI schema.

    Steps to Use Dynamic Bearer Token with Connection Authentication

    A. Modify OpenAPI Security Scheme (keep this)

    "securitySchemes": {

      "BearerAuth": {

        "type": "apiKey",

        "name": "Authorization",

        "in": "header",

        "description": "JWT Bearer token. Format: Bearer {token}"

      }

    }

    B. Add Security Requirement

    "security": [

      {

        "BearerAuth": []

      }

    ]

    C. Use Azure AI Agent Service’s Connection Feature

    • In Azure AI Foundry, go to your tool → Connections tab
    • Select “Add Connection” → Choose type “API Key”
    • Set:
      • Header Name: Authorization
        • Header Value Template: Bearer {{token}}
          • Replace {{token}} with a key vault reference or a logic app URL that returns a token dynamically

    D. Token Generation Options

    If your API requires a short-lived JWT:

    1. Option 1: Use Azure Logic App or Azure Function to generate and return a fresh token.
    2. Option 2: Manually paste token in connection for now if automation is not in place.

    E. Testing the Connection

    In Azure AI Foundry Playground, test your agent with:

    • Network access working (step 1 solved)
    • Connection set with Authorization header using the live token

    If this fails, the curl error should show the actual root cause:

    • 403 → token issue
    • 404 → wrong endpoint
    • timeout or aiohttp client error → VNet/firewall issue

    Summary Checklist

    Task Status
    Private API reachable from Azure AI Foundry? Ensure via VNet, APIM, or Private Link
    OpenAPI securitySchemes defined correctly? Yes
    Connection used for Authorization: Bearer {{token}}? Must do
    Token rotation setup? Optional but ideal
    Agent tool configured with the right connection? Verify in Azure Portal

    Best Regards,

    Jerald Felix

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.