You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/app-service/tutorial-ai-openai-chatbot-node.md
+76-77Lines changed: 76 additions & 77 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,9 @@
1
1
---
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.
4
4
author: jefmarti
5
5
ms.author: jefmarti
6
-
ms.date: 05/19/2025
6
+
ms.date: 03/03/2026
7
7
ms.update-cycle: 180-days
8
8
ms.topic: tutorial
9
9
ms.custom:
@@ -12,21 +12,24 @@ ms.custom:
12
12
- build-2025
13
13
ms.collection: ce-skilling-ai-copilot
14
14
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.
15
16
---
16
17
17
18
# Tutorial: Build a chatbot with Azure App Service and Azure OpenAI (Express.js)
18
19
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.
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.
22
23
23
-
In this tutorial, you learn how to:
24
+
You learn how to:
24
25
25
26
> [!div class="checklist"]
26
27
> * Create an Azure OpenAI resource and deploy a language model.
27
28
> * Build an Express.js application that connects to Azure OpenAI.
28
29
> * 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.
@@ -39,25 +42,25 @@ In this tutorial, you learn how to:
39
42
40
43
## 2. Create and set up an Express.js web app
41
44
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.
43
46
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
+
```
49
52
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.
51
54
52
-
1. Back inthe Codespace terminal, stop the app with Ctrl+C.
55
+
1.In the codespace terminal, stop the app by pressing Ctrl+C.
53
56
54
-
1. Install the NPM dependencies for working with Azure OpenAI:
57
+
1. Install the NPM dependencies for working with Azure OpenAI.
55
58
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.
61
64
62
65
```html
63
66
<!DOCTYPE html>
@@ -88,7 +91,7 @@ In this tutorial, you learn how to:
88
91
</html>
89
92
```
90
93
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:
92
95
93
96
```javascript
94
97
var express = require('express');
@@ -146,74 +149,70 @@ In this tutorial, you learn how to:
146
149
--output tsv
147
150
```
148
151
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:
1. Select **Open in browser** to launch the app in a new browser tab. Submit a question and seeif 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.
156
159
157
160
## 3. Deploy to Azure App Service and configure OpenAI connection
158
161
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.
173
163
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.
175
165
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
+
```
177
176
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:
187
178
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.
189
182
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
+
```
193
192
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.
195
194
196
-
```azurecli
197
-
az webapp browse
198
-
```
195
+
```azurecli
196
+
az webapp browse
197
+
```
199
198
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.
Your app is now deployed and connected to Azure OpenAI with managed identity.
205
204
206
205
## Frequently asked questions
207
206
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)
211
210
212
211
---
213
212
214
-
### What if I want to connect to OpenAI instead of Azure OpenAI?
213
+
### How can I connect to OpenAI instead of Azure OpenAI?
215
214
216
-
To connect to OpenAI instead, use the following code:
215
+
To connect to OpenAI instead of Azure OpenAI, use the following code:
217
216
218
217
```javascript
219
218
const { OpenAI } =require('openai');
@@ -225,31 +224,31 @@ const client = new OpenAI({
225
224
226
225
For more information, see [OpenAI API authentication](https://platform.openai.com/docs/api-reference/authentication).
227
226
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.
229
229
230
230
---
231
231
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?
233
233
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).
235
235
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.
237
238
238
239
---
239
240
240
-
### How does DefaultAzureCredential work in this tutorial?
241
+
### How does the DefaultAzureCredential work?
241
242
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.
243
244
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.
246
247
247
248
This approach lets your code run securely and seamlessly in both local and cloud environments without modification.
248
249
249
-
## Next steps
250
+
## Related content
250
251
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)
252
253
-[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)
0 commit comments