Skip to content

Commit d953c51

Browse files
authored
Merge pull request #312564 from v-thepet/apps6
Freshness Edit: App Service 6
2 parents 9828bf5 + 999dd3e commit d953c51

2 files changed

Lines changed: 76 additions & 77 deletions

File tree

-8.22 KB
Loading

articles/app-service/tutorial-ai-openai-chatbot-node.md

Lines changed: 76 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
---
2-
title: Intelligent app with Azure OpenAI (Express.js)
3-
description: Learn how to build and deploy a Node.js web app to Azure App Service that connects to Azure OpenAI using managed identity.
2+
title: Build and deploy an Azure OpenAI (Express.js) chatbot
3+
description: Learn how to build a Node.js web app that connects to Azure OpenAI and deploy it to Azure App Service using managed identity.
44
author: jefmarti
55
ms.author: jefmarti
6-
ms.date: 05/19/2025
6+
ms.date: 03/03/2026
77
ms.update-cycle: 180-days
88
ms.topic: tutorial
99
ms.custom:
@@ -12,21 +12,24 @@ ms.custom:
1212
- build-2025
1313
ms.collection: ce-skilling-ai-copilot
1414
ms.service: azure-app-service
15+
#customer intent: As a Node.js web app developer, I want to build an intelligent AI application that uses Azure OpenAI and deploy it to Azure App Service so I can offer a cloud chatbot app to my users.
1516
---
1617

1718
# Tutorial: Build a chatbot with Azure App Service and Azure OpenAI (Express.js)
1819

19-
In this tutorial, you'll build an intelligent AI application by integrating Azure OpenAI with a Node.js application and deploying it to Azure App Service. You'll create an Express app with a view and a controller that sends chat completion requests to a model in Azure OpenAI.
20+
In this tutorial, you build an intelligent AI application by integrating Azure OpenAI with a Node.js web application, and deploy it to Azure App Service.
2021

21-
:::image type="content" source="media/tutorial-ai-openai-chatbot-nodejs/chat-in-browser.png" alt-text="Screenshot showing chatbot running in Azure App Service.":::
22+
You create an Express app with a view and a controller that sends chat completion requests to a model in Azure OpenAI. You connect to the app in Azure by using a managed identity.
2223

23-
In this tutorial, you learn how to:
24+
You learn how to:
2425

2526
> [!div class="checklist"]
2627
> * Create an Azure OpenAI resource and deploy a language model.
2728
> * Build an Express.js application that connects to Azure OpenAI.
2829
> * Deploy the application to Azure App Service.
29-
> * Implement passwordless secure authentication both in the development environment and in Azure.
30+
> * Implement passwordless secure authentication in the development environment and in Azure.
31+
32+
:::image type="content" source="media/tutorial-ai-openai-chatbot-nodejs/chat-in-browser.png" alt-text="Screenshot showing chatbot running in Azure App Service.":::
3033

3134
## Prerequisites
3235

@@ -39,25 +42,25 @@ In this tutorial, you learn how to:
3942

4043
## 2. Create and set up an Express.js web app
4144

42-
1. In your Codespace terminal, create an Express.js template in the workspace and try running it the first time.
45+
1. In your codespace terminal, create an Express.js template in the workspace and run it.
4346

44-
```bash
45-
npx express-generator . --view ejs
46-
npm audit fix --force
47-
npm install && npm start
48-
```
47+
```bash
48+
npx express-generator . --view ejs
49+
npm audit fix --force
50+
npm install && npm start
51+
```
4952

50-
You should see a notification in GitHub Codespaces indicating that the app is available at a specific port. Select **Open in browser** to launch the app in a new browser tab.
53+
1. You see a notification in GitHub Codespaces that the app is available at a specific port. Select **Open in browser** to launch the app in a new browser tab.
5154

52-
1. Back in the Codespace terminal, stop the app with Ctrl+C.
55+
1. In the codespace terminal, stop the app by pressing Ctrl+C.
5356

54-
1. Install the NPM dependencies for working with Azure OpenAI:
57+
1. Install the NPM dependencies for working with Azure OpenAI.
5558

56-
```bash
57-
npm install openai @azure/openai @azure/identity
58-
```
59-
60-
1. Open *views/index.ejs* and replace it with the following code, for a simple chat interface.
59+
```bash
60+
npm install openai @azure/openai @azure/identity
61+
```
62+
63+
1. In the file tree, open *views/index.ejs* and replace its contents with the following code for a simple chat interface.
6164

6265
```html
6366
<!DOCTYPE html>
@@ -88,7 +91,7 @@ In this tutorial, you learn how to:
8891
</html>
8992
```
9093

91-
1. Open routes/index.js and replace its content with the following code, for a simple chat completion call with Azure OpenAI:
94+
1. Open *routes/index.js* and replace its content with the following code for a simple chat completion call with Azure OpenAI:
9295

9396
```javascript
9497
var express = require('express');
@@ -146,74 +149,70 @@ In this tutorial, you learn how to:
146149
--output tsv
147150
```
148151

149-
1. Run the app again by adding `AZURE_OPENAI_ENDPOINT` with its value from the CLI output:
152+
1. Run the app by adding `AZURE_OPENAI_ENDPOINT` with its value from the preceding CLI output:
150153

151154
```bash
152155
AZURE_OPENAI_ENDPOINT=<output-from-previous-cli-command> npm start
153156
```
154157

155-
1. Select **Open in browser** to launch the app in a new browser tab. Submit a question and see if you get a response message.
158+
1. Select **Open in browser** to launch the app in a new browser tab. Submit a question to see a response message.
156159

157160
## 3. Deploy to Azure App Service and configure OpenAI connection
158161

159-
Now that your app works locally, let's deploy it to Azure App Service and set up a service connection to Azure OpenAI using managed identity.
160-
161-
1. First, deploy your app to Azure App Service using the Azure CLI command `az webapp up`. This command creates a new web app and deploys your code to it:
162-
163-
```azurecli
164-
az webapp up \
165-
--resource-group $RESOURCE_GROUP \
166-
--location $LOCATION \
167-
--name $APPSERVICE_NAME \
168-
--plan $APPSERVICE_NAME \
169-
--sku B1 \
170-
--os-type Linux \
171-
--track-status false
172-
```
162+
Now that your app works locally, deploy it to Azure App Service and set up a service connection to Azure OpenAI using managed identity.
173163

174-
This command might take a few minutes to complete. It creates a new web app in the same resource group as your OpenAI resource.
164+
1. First, deploy your app to Azure App Service using the Azure CLI command `az webapp up`. This command creates a new web app in the same resource group as your OpenAI resource and deploys your code to it. The command might take a few minutes to complete.
175165

176-
1. After the app is deployed, create a service connection between your web app and the Azure OpenAI resource using managed identity:
166+
```azurecli
167+
az webapp up \
168+
--resource-group $RESOURCE_GROUP \
169+
--location $LOCATION \
170+
--name $APPSERVICE_NAME \
171+
--plan $APPSERVICE_NAME \
172+
--sku B1 \
173+
--os-type Linux \
174+
--track-status false
175+
```
177176

178-
```azurecli
179-
az webapp connection create cognitiveservices \
180-
--resource-group $RESOURCE_GROUP \
181-
--name $APPSERVICE_NAME \
182-
--target-resource-group $RESOURCE_GROUP \
183-
--account $OPENAI_SERVICE_NAME \
184-
--connection azure-openai \
185-
--system-identity
186-
```
177+
1. After the app is deployed, create a service connection between your web app and the Azure OpenAI resource using managed identity. The following command creates a connection between your web app and the Azure OpenAI resource by:
187178

188-
This command creates a connection between your web app and the Azure OpenAI resource by:
179+
- Generating system-assigned managed identity for the web app.
180+
- Adding the **Cognitive Services OpenAI Contributor** role to the managed identity for the Azure OpenAI resource.
181+
- Adding the `AZURE_OPENAI_ENDPOINT` app setting to your web app.
189182

190-
- Generating system-assigned managed identity for the web app.
191-
- Adding the Cognitive Services OpenAI Contributor role to the managed identity for the Azure OpenAI resource.
192-
- Adding the `AZURE_OPENAI_ENDPOINT` app setting to your web app.
183+
```azurecli
184+
az webapp connection create cognitiveservices \
185+
--resource-group $RESOURCE_GROUP \
186+
--name $APPSERVICE_NAME \
187+
--target-resource-group $RESOURCE_GROUP \
188+
--account $OPENAI_SERVICE_NAME \
189+
--connection azure_openai \
190+
--system-identity
191+
```
193192

194-
1. Open the deployed web app in the browser. Find the URL of the deployed web app in the terminal output. Open your web browser and navigate to it.
193+
1. Find the URL of your deployed app in the terminal output from the `az webapp up` command, and navigate to the app in your web browser.
195194

196-
```azurecli
197-
az webapp browse
198-
```
195+
```azurecli
196+
az webapp browse
197+
```
199198

200-
1. Type a message in the textbox and select **Send**, and give the app a few seconds to reply with the message from Azure OpenAI.
199+
1. In your web app, enter a message in the textbox and select **Send**. Give the app a few seconds to reply with the message from Azure OpenAI.
201200

202-
:::image type="content" source="media/tutorial-ai-openai-chatbot-nodejs/chat-in-browser.png" alt-text="Screenshot showing chatbot running in Azure App Service.":::
201+
:::image type="content" source="media/tutorial-ai-openai-chatbot-nodejs/chat-in-browser.png" alt-text="Screenshot showing chatbot running in Azure App Service.":::
203202

204203
Your app is now deployed and connected to Azure OpenAI with managed identity.
205204

206205
## Frequently asked questions
207206

208-
- [What if I want to connect to OpenAI instead of Azure OpenAI?](#what-if-i-want-to-connect-to-openai-instead-of-azure-openai)
209-
- [Can I connect to Azure OpenAI with an API key instead?](#can-i-connect-to-azure-openai-with-an-api-key-instead)
210-
- [How does DefaultAzureCredential work in this tutorial?](#how-does-defaultazurecredential-work-in-this-tutorial)
207+
- [How can I connect to OpenAI instead of Azure OpenAI?](#how-can-i-connect-to-openai-instead-of-azure-openai)
208+
- [Can I connect to Azure OpenAI with an API key instead of managed identity?](#can-i-connect-to-azure-openai-with-an-api-key-instead-of-managed-identity)
209+
- [How does the DefaultAzureCredential work?](#how-does-the-defaultazurecredential-work)
211210

212211
---
213212

214-
### What if I want to connect to OpenAI instead of Azure OpenAI?
213+
### How can I connect to OpenAI instead of Azure OpenAI?
215214

216-
To connect to OpenAI instead, use the following code:
215+
To connect to OpenAI instead of Azure OpenAI, use the following code:
217216

218217
```javascript
219218
const { OpenAI } = require('openai');
@@ -225,31 +224,31 @@ const client = new OpenAI({
225224

226225
For more information, see [OpenAI API authentication](https://platform.openai.com/docs/api-reference/authentication).
227226

228-
When working with connection secrets in App Service, you should use [Key Vault references](app-service-key-vault-references.md) instead of storing secrets directly in your codebase. This ensures that sensitive information remains secure and is managed centrally.
227+
>[!IMPORTANT]
228+
>When working with connection secrets like API keys in App Service, you should [use Azure Key Vault references](app-service-key-vault-references.md) instead of storing secrets directly in your code. This practice ensures that sensitive information remains secure and is managed centrally.
229229
230230
---
231231

232-
### Can I connect to Azure OpenAI with an API key instead?
232+
### Can I connect to Azure OpenAI with an API key instead of managed identity?
233233

234-
Yes, you can connect to Azure OpenAI using an API key instead of managed identity. For more information, see the [Azure OpenAI JavaScript quickstart](/azure/ai-services/openai/chatgpt-quickstart?pivots=programming-language-javascript).
234+
Yes, you can connect to Azure OpenAI using an API key instead of managed identity. For more information, see [Use the Azure OpenAI Responses API](/azure/ai-services/openai/chatgpt-quickstart?pivots=programming-language-javascript).
235235

236-
When working with connection secrets in App Service, you should use [Key Vault references](app-service-key-vault-references.md) instead of storing secrets directly in your codebase. This ensures that sensitive information remains secure and is managed centrally.
236+
>[!IMPORTANT]
237+
>When working with connection secrets like API keys in App Service, you should [use Key Vault references](app-service-key-vault-references.md) instead of storing secrets directly in your code. This practice ensures that sensitive information remains secure and is managed centrally.
237238
238239
---
239240

240-
### How does DefaultAzureCredential work in this tutorial?
241+
### How does the DefaultAzureCredential work?
241242

242-
The `DefaultAzureCredential` simplifies authentication by automatically selecting the best available authentication method:
243+
The `DefaultAzureCredential` simplifies authentication by automatically selecting the best available authentication method.
243244

244-
- **During local development**: After you run `az login`, it uses your local Azure CLI credentials.
245-
- **When deployed to Azure App Service**: It uses the app's managed identity for secure, passwordless authentication.
245+
- During local development, after you run `az login`, the `DefaultAzureCredential` uses your local Azure CLI credentials.
246+
- For Azure App Service deployments, the `DefaultAzureCredential` uses the app's managed identity for secure, passwordless authentication.
246247

247248
This approach lets your code run securely and seamlessly in both local and cloud environments without modification.
248249

249-
## Next steps
250+
## Related content
250251

251-
- [Tutorial: Build a Retrieval Augmented Generation with Azure OpenAI and Azure AI Search (Express.js)](tutorial-ai-openai-search-nodejs.md)
252+
- [Tutorial: Build a retrieval augmented generation app in Azure App Service with Azure OpenAI and Azure AI Search (Express.js)](tutorial-ai-openai-search-nodejs.md)
252253
- [Tutorial: Run chatbot in App Service with a Phi-4 sidecar extension (Express.js)](tutorial-ai-slm-expressjs.md)
253-
- [Create and deploy an Azure OpenAI Service resource](/azure/ai-services/openai/how-to/create-resource)
254-
- [Configure Azure App Service](/azure/app-service/configure-common)
255-
- [Enable managed identity for your app](overview-managed-identity.md)
254+
- [Use managed identities for App Service and Azure Functions](overview-managed-identity.md)

0 commit comments

Comments
 (0)