Your profile

One profile for every MSDEVBUILD site. Only you can see it, and the site owner if you write in.

Lesson 16 of 39
❮Prev Next❯

Foundry SDKs and Connectors

Module 2: GenAI & Agents ⏱ 11 min read Updated

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    30-second answer

    The Foundry SDK (azure-ai-projects) opens a project with AIProjectClient, using the project endpoint and DefaultAzureCredential. From it you get an OpenAI-compatible client, the project's deployments, agents and connections. A connection stores the target and authentication of another resource, such as Azure AI Search or Application Insights, so your code reads it by name instead of holding a URL or a key.

    Simulate it » No Azure account needed

    Exam objective: 2.1.5 Integrate generative workflows into applications by using Foundry SDKs and connectors. (Skills measured as of April 16, 2026)

    Simulations

    Feel it before you read it. No account needed, nothing leaves this page.

    Azure portal: Find the project endpoint and use it from the SDK Copy the project endpoint and add a connection.
    </> Embed

    Embed this simulation

    Free for trainers, teachers and bloggers. Paste this into your page:

    Loading the portal…

    The project <strong>Overview</strong> shows the <strong>project endpoint</strong>. That one URL is what <code>AIProjectClient</code> needs.

    👆 Click the glowing item in the portal to continue

    Read the steps as text
    1. The project Overview shows the project endpoint. That one URL is what AIProjectClient needs.
    2. Keep the endpoint in an environment variable and sign in with DefaultAzureCredential. Your own sign-in works on your laptop; a managed identity works in Azure.
    3. A connection stores a target and how to authenticate to it. The app reads it with project.connections.get("search-contoso") instead of holding the URL or a key.
    4. Connecting Application Insights lets the SDK find where traces should go. Later lessons use it.

    🧪 Simulation for learning. Screens are simplified versions of the Azure portal; names, prices and IDs are examples. Nothing here touches real Azure.

    😟 THE PROBLEM

    An API key and a search URL were pasted into the repo, and every environment needs a code change.

    😊 THE SOLUTION

    Use the project endpoint, Entra ID sign-in and connections.

    • Keys in code✓ DefaultAzureCredential
    • URLs copied per environment✓ Project connections
    • Different clients for each service✓ AIProjectClient with the OpenAI-compatible client
    🛠️ Solved with: Foundry SDK (azure-ai-projects)

    Start with a question

    Why does a hotel give you a key card instead of the master key?

    A key card opens your room and nothing else, and the front desk can cancel it. A master key opens everything and, if it’s lost, every lock is at risk.

    An API key pasted in code is the master key. Microsoft Entra ID with a managed identity is the key card: the app proves who it is, gets a short-lived token, and only the rooms its role allows. The Foundry SDK is built around that.

    Explain it like I’m 10

    The technical explanation

    One client, many doors

    # pip install "azure-ai-projects>=2.3.0" azure-identity
    import os
    from azure.identity import DefaultAzureCredential
    from azure.ai.projects import AIProjectClient
    
    project = AIProjectClient(endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],   # .../api/projects/<project>
                              credential=DefaultAzureCredential())
    
    openai = project.get_openai_client()                 # model calls with deployment names
    for d in project.deployments.list():                 # what is deployed
        print(d.name, d.model_name)
    for c in project.connections.list():                 # what the project can reach
        print(c.name, c.type, c.target)
    You wantUse
    Call a modelproject.get_openai_client() then responses.create(model=<deployment>)
    List deploymentsproject.deployments.list()
    Reach another resourceproject.connections.get("search-contoso")
    Create and run agentsproject.agents and the agent client
    Find the trace destinationThe project’s telemetry helpers (Application Insights connection string)

    The package also exists for JavaScript, .NET and Java, and the same operations are available over REST, as of 25 November 2026.

    Connections keep secrets out of code

    Create a connection in the portal (Settings > Connected resources) or with the CLI. A connection has a category (for example Azure AI Search or Application Insights), a target and an authentication type. Prefer Microsoft Entra ID. Then grant the right role: the project’s managed identity needs Search Index Data Reader on the search service, for example.

    az cognitiveservices account project connection create -n contoso-agents -g rg-agents \
      --project-name order-desk --connection-name search-contoso --file search-connection.yml
    conn = project.connections.get("search-contoso")     # name in, target out
    print(conn.target)                                   # https://srch-contoso-agents.search.windows.net

    Two endpoints, don’t mix them

    The project endpoint (.../api/projects/<project>) is for AIProjectClient. The resource endpoint (https://<resource>.services.ai.azure.com/) is what Foundry Tools and some evaluator settings expect. When a call can’t find anything, check you used the right one.

    Architect level Local development to production

    Locally, DefaultAzureCredential can use your Azure CLI or Visual Studio Code sign-in. In Azure it uses the managed identity assigned to the host. Give each environment its own project and its own endpoint in an environment variable, and keep the identity roles in your infrastructure code. The application code then stays identical from laptop to production.

    Real-world business scenario

    Wholesale

    Northwind Traders (fictional)

    The problem
    Search URLs and an API key were copied into five services. A key leaked in a public repository, and every environment needed code edits.
    What they used
    One Foundry project per environment, a connection called search-contoso with Entra ID authentication, a managed identity per service with Search Index Data Reader, and code that reads the connection through AIProjectClient.
    Why that choice
    No key remains to leak, the same code runs in every environment, and rotating or moving the search service means editing one connection.
    The result
    The leaked key was revoked with no outage, and new services connected by name in minutes.

    When to use it and when not to

    ✓ Use it when…

    • Your app calls Foundry models, agents or evaluations from code.
    • Several services need the same search or monitoring resource.
    • You want the same code on a laptop and in Azure.

    ✕ Think twice when…

    • You would store a key in code or a config file.
    • A single no-code playground test is enough.
    • You need a service the project has no connection or tool for. Call it directly with its own SDK.

    ↔ Alternatives to consider

    • The OpenAI SDK directly against the resource endpoint for model calls only.
    • Microsoft Agent Framework for code-first multi-agent apps.
    • REST calls when no SDK fits.
    Ready for more?The full lesson adds the technical explanation, when to use it, interview questions and revision notes.

    Test yourself with exercises

    5 original practice questions in the AI-103 style. Pick your answers, then check them. You will see why each option is right or wrong. Score 75% or more and this topic is marked complete.

    Question 1

    Question 1 of 5

    A Python app must call Azure AI Search without hard-coding its URL or credentials. The search service is connected to the Foundry project.

    How should the app obtain the search endpoint?

    Question 2

    Question 2 of 5

    The same code must run on a developer laptop and in Azure without any key.

    Which credential should the code use?

    Question 3

    Question 3 of 5

    Which Python package provides AIProjectClient?

    Question 4

    Question 4 of 5

    A developer wants to call a model through the Responses API by using the project client.

    Which call returns an OpenAI-compatible client?

    Question 5

    Question 5 of 5

    An agent app must export traces to Application Insights, and the instrumentation needs the connection string.

    Where can the app obtain it through the SDK?

    Interview preparation

    Say your answer out loud first, then open the model answer. Interviewers care more about your reasoning than the exact words.

    Beginner What is a connection in Foundry?

    Model answer

    It is a saved link from the project to another resource, like Azure AI Search, with its address and how to sign in. The app asks the project for it by name, so the address and the secret stay out of the code.

    Intermediate How does the same code run on a laptop and in Azure?

    Model answer

    I create the client with DefaultAzureCredential. On the laptop it uses my signed-in account. In Azure it uses the managed identity of the host. The code doesn't change and holds no key.

    Experienced developer What do you do when the SDK call returns 403?

    Model answer

    I check the role assignment for the identity on the Foundry resource and, for a connection, on the target resource too. For example the project identity needs Search Index Data Reader on the search service. Then I confirm the endpoint is for the right project.

    Scenario A teammate pasted a key into code to reach Azure AI Search. What do you change?

    Model answer

    I create a project connection to the search service with Microsoft Entra ID authentication, assign the right role, and change the code to read the connection by name and sign in with DefaultAzureCredential. Then I rotate the key that leaked.

    Quick revision

    Key points

    • AIProjectClient takes the project endpoint and a credential.
    • get_openai_client() returns the client for model calls.
    • Connections hold targets and authentication for other resources.
    • DefaultAzureCredential keeps the same code across environments.
    • Give the identity the roles it needs on every resource it touches.

    Important terminology

    AIProjectClient
    The main class in azure-ai-projects. It connects to one Foundry project through its endpoint.
    Connection
    A project setting that stores the target and authentication method of another resource, such as Azure AI Search.
    Project endpoint
    The URL for one project, used to create the client.
    Managed identity
    An Azure-managed identity that an app uses to get Microsoft Entra ID tokens without a stored secret.
    OpenAI-compatible client
    A client from get_openai_client() that you call with the OpenAI Responses API shape, using deployment names.
    Connector
    Informal word for a way to reach another service from a project, such as a connection or a tool.

    Common mistakes

    • Hard-coding service URLs and keys.
    • Forgetting the role assignment, then blaming the SDK for a 403.
    • Mixing up the resource endpoint and the project endpoint.

    Exam tips

    • "Read the Azure AI Search endpoint without hard-coding it" = project.connections.get(name).
    • "Same code on a laptop and in Azure with no key" = DefaultAzureCredential.
    • "Package that contains AIProjectClient" = azure-ai-projects.

    Five quick revision questions

    1. 1.Which package has AIProjectClient?Show

      azure-ai-projects.

    2. 2.What does project.get_openai_client() return?Show

      An OpenAI-compatible client.

    3. 3.What does a connection store?Show

      A target and its authentication method.

    4. 4.Which credential keeps code key-free?Show

      DefaultAzureCredential.

    5. 5.Which value creates the client?Show

      The project endpoint.

    Official references

    Finished this lesson? Tick it off. Your progress is saved in this browser.

    Next up: Design an Agent: Role, Goals, Threads and Tool Schemas

    Studying with a friend? Send them this lesson

    It's free and needs no sign-up. Their progress saves in their own browser.

    Lesson done? Make it stick with a quick gameSort It, Match the Pairs, Lightning Round and Who Am I? Earn XP and badges. Play ❯

    Install MSDEVBUILD Azure

    Free · no account · works offline

    Save the whole course for offline ›

    Hi, I’m your Nimbus

    Your study buddy. Pick what we do next.

    MSDEVBUILD · Multi-Stack Dev Build

    Sign in to MSDEVBUILD

    One account for Azure certifications and the blog: sign in here and you are signed in on both. Your XP, streak, practice deck and certificate stay in sync on every device. Optional: everything works without an account.