Code examples
Start with a request. Build in the language you already use.
Before you run an example
Activate your subscription, connect a supported provider, and set
Activate your subscription, connect a supported provider, and set
DCA_GATEWAY_KEY in your server environment. Python also uses DCA_DEPLOYMENT for your deployment name and requires the openai package. Keep keys out of browser and mobile application code.POST
bash
curl https://ai-gateway-east-us1.azure-api.net/gateway/openai/v1/chat/completions \
-H "api-key: $DCA_GATEWAY_KEY" \
-H "Content-Type: application/json" \
-d '{"messages":[{"role":"user","content":"Hello, DCAI."}]}'python
import os
from openai import OpenAI
key = os.environ["DCA_GATEWAY_KEY"]
client = OpenAI(
base_url="https://ai-gateway-east-us1.azure-api.net/gateway/openai/v1",
api_key=key,
default_headers={"api-key": key},
)
response = client.chat.completions.create(
model=os.environ["DCA_DEPLOYMENT"],
messages=[{"role": "user", "content": "Hello, DCAI."}],
)
print(response.choices[0].message.content)javascript
// Run on your server (Node.js), with the key in your environment.
const response = await fetch(
"https://ai-gateway-east-us1.azure-api.net/gateway/openai/v1/chat/completions",
{
method: "POST",
headers: {
"api-key": process.env.DCA_GATEWAY_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify({
messages: [{ role: "user", content: "Hello, DCAI." }],
}),
},
);
if (!response.ok) throw new Error(await response.text());
console.log(await response.json()); Your provider. Your deployment. One DCAI key.
Complete sample collection
The repository includes streaming, embeddings, Responses, Claude, route selection and connection checks. Python examples use the shared _gateway.py helper; download the complete sample set or clone the public repository.
python/chat.pyDownload ↓python/stream.pyDownload ↓python/embeddings.pyDownload ↓python/responses_api.pyDownload ↓python/claude.pyDownload ↓python/_gateway.pyDownload ↓python/requirements.txtDownload ↓javascript/chat.mjsDownload ↓javascript/claude.mjsDownload ↓javascript/package.jsonDownload ↓curl/chat.shDownload ↓curl/claude.shDownload ↓curl/routes.shDownload ↓Repository setup instructions
Every sample reads your key from the environment and was run against the live gateway before publication.
bash
export DCA_GATEWAY_KEY="<your gateway key>"
export DCA_DEPLOYMENT="<a deployment in your Foundry, for example gpt-4.1-mini>"| Sample | What it shows |
|---|---|
python/chat.py |
Chat completion with the OpenAI SDK, and the gateway's response headers |
python/routes.py |
x-dca-route: the gateway picks your mapped deployment |
python/stream.py |
Streaming |
python/azure_openai_sdk.py |
Existing Azure OpenAI code, unchanged |
python/responses_api.py |
The Responses API |
python/embeddings.py |
Embeddings (DCA_EMBEDDING_DEPLOYMENT) |
python/claude.py |
Claude with the Anthropic SDK (DCA_CLAUDE_DEPLOYMENT) |
python/check_connection.py |
One request and a plain-language verdict, for troubleshooting |
javascript/chat.mjs |
Chat completion with the OpenAI Node.js SDK |
javascript/claude.mjs |
Claude with the Anthropic Node.js SDK |
curl/chat.sh, curl/routes.sh, curl/us-boundary.sh, curl/claude.sh |
The same with curl |
connect/connect-foundry.sh |
One-time setup in your tenant: the connector and its role |
Python
bash
python3 -m venv .venv && . .venv/bin/activate
pip install -r samples/python/requirements.txt
python3 samples/python/chat.pyNode.js 18 or later
bash
cd samples/javascript && npm install && node chat.mjs