Forum Discussion

AyusmanBasu0604's avatar
1 year ago
Solved

Unable to Connect Azure AI Foundry Agent from Fabric Notebook

I am trying to Invoke an Agent that I created within my Azure AI Foundry project using a notebook in Fabric. I have used the correct API Key from the Azure Foundry portal based on the correct endpoin...
  • tayloramy's avatar
    1 year ago

    Hi AyusmanBasu0604,

     

    A 401 from the Azure AI Foundry project endpoint (*.services.ai.azure.com) almost always comes down to one of four things:

    1. Mismatched endpoint vs. key - using a key from one resource (or project) with a different endpoint/region.
    2. Wrong auth header - sending the key in Authorization: instead of the required api-key: header.
    3. Local auth (keys) disabled on the Foundry resource, so all key-based calls are refused.
    4. Workspace outbound network controls (Fabric) blocking or rewriting the call.

    Quick solution

    • Use the project-scoped base URL and key from the same Azure AI Foundry project:
      https://<foundry-resource>.services.ai.azure.com/api/projects/<project-name>
      (Find them in the Foundry portal under your Project > Settings > Keys and endpoints.)
    • Call the Agents REST API with the latest supported api-version, and send the key in api-key (not Authorization).
    • If your org has enabled Fabric Workspace Outbound Access Protection, ask an admin to allowlist *.services.ai.azure.com.
    • If the resource has Local authentication disabled, switch to Entra ID auth instead of keys.

    Why your DNS test failed

    socket.gethostbyname() expects a host name, not a full URL. Drop the https:// and any path:

    import socket
    socket.gethostbyname("<foundry-resource>.services.ai.azure.com")

    Minimal Python example (Fabric notebook)

    This pings the Agents API “whoami” equivalent (list agents) to verify auth/route:

    import os, requests
    
    PROJECT_BASE = "https://<foundry-resource>.services.ai.azure.com/api/projects/<project-name>"
    API_VERSION  = "2024-10-01-preview"   # check current version in docs
    API_KEY      = os.environ.get("AZURE_AI_FOUNDRY_KEY") or "<paste-temporarily-for-test>"
    
    headers = {
        "api-key": API_KEY,                          # IMPORTANT: use api-key header
        "Content-Type": "application/json"
    }
    
    # Example: list agents
    url = f"{PROJECT_BASE}/agents?api-version={API_VERSION}"
    r = requests.get(url, headers=headers, timeout=30)
    print(r.status_code, r.text)

    If this returns 200, your auth and networking are good; any later 401s come from a different URL/key mix-up.

    Common gotchas I’ve seen

    Fabric networking note

    If your Fabric admin has enabled Workspace Outbound Access Protection, your notebook’s egress may be restricted. Ask them to allow *.services.ai.azure.com for your workspace:

    If you still see 401 after the steps above, grab:

    • The exact URL you’re calling (mask project name),
    • The auth method (key vs. Entra ID),
    • The response headers, and
    • Confirmation that your key came from the same Foundry project as the endpoint, in the same region.

    I’ve seen these four items resolve 90%+ of cases.

    If you found this helpful, consider giving some Kudos. If I answered your question or solved your problem, mark this post as the solution.