You can create an API-based message extension using Developer Portal for Teams and Teams Toolkit for Visual Studio Code, command line interface (CLI), or Visual Studio.
Enter a name of the app and select the Manifest version as Latest prerelease (devPreview).
Select Add.
In the left pane, under Configure, update the following Basic information:
Full name
Short description
Long description
Developer or company name
Website (must be a valid HTTPS URL)
Privacy policy
Terms of use
Select Save.
Select App features.
Select Messaging extension.
Under Message extension type, select API.
If you get a disclaimer, which reads Bot message extension is already in use by users. Would you like to change message extension type to API?. Select Yes, change.
Under OpenAPI spec, select Upload now.
Select the OpenAPI Description document in the JSON or YAML format and select Open.
Select Save. A pop-up appears with the message API spec saved successfully.
Select Got it.
Add commands
Note
Message extensions built from an API only support a single parameter.
You can add commands and parameters to your message extension, to add commands:
Under Message extension type, select Add.
An Add command pop-up appears with a list of all the available APIs from the OpenAPI Description document.
Select an API from the list and select Next.
A Command details appears.
Under Command details, go to Adaptive card template and select Upload now.
Note
If you have more than one API, ensure that you upload the Adaptive card template for each API.
Select the Adaptive Card template file in JSON format and select Open.
The following attributes are updated automatically from the Adaptive Card template:
Command Type
Command ID
Command title
Parameter name
Parameter description
Under Details, update the Command description.
If you want to launch a command using a trigger in Microsoft 365 Copilot, turn on the Automatically run the command when a user opens the extension toggle.
Select Add. The command is added successfully.
Select Save.
An API-based message extension is created.
To test your API-based message extension created in the Developer Portal for Teams, you can use the following methods:
Preview in Teams: In Developer Portal, open your message extension and select Preview in Teams in the upper-right corner. You're redirected to Teams, where you can add the app to Teams to preview the app.
Download app package: On the message extension page, select App package from the left pane and then, in the upper-left corner of the window, select Download app package. The app package is downloaded to your local machine in a .zip file. You can upload the app package to teams and test the message extension.
Note
Teams Toolkit supports OpenAPI Specification version 2.0 and 3.0.x.
To build an API-based message extension using Teams Toolkit for Visual Studio Code, follow these steps:
Open Visual Studio Code.
From the left pane, Select Teams Toolkit.
Select Create a New App.
Select Message Extension.
Select Custom Search Results.
Select one of the following options:
To build from the beginning, select Start with a new API.
If you already have an OpenAPI description document, select Start with an OpenAPI Description Document.
Based on the options selected in step 6, select the following:
The authentication flow for Microsoft Entra is only functional in remote environments. You can't test it in a local environment due to the lack of authentication support in Azure Function core tools. The repair API can be invoked anonymously in a local environment.
Select the authentication type:
None: Select if you don't want any authentication for the user to access the API.
API Key: Select if you want to authenticate using an API key.
Microsoft Entra: Select if you want to authenticate using app user's Teams identity.
Select a programming language.
Select Default folder.
Enter the name of your app and select Enter. Teams Toolkit creates a new plugin with API from Azure functions.
To get started, you must update the source code in the following files:
File
Contents
src/functions/repair.ts
The main file of a function in Azure Functions. Defines an Azure Function that retrieves and filters repair records based on a query parameter from an HTTP GET request, and returns the results as a JSON response.
src/repairsData.json
The data source for the repair API.
src/keyGen.ts
Designed to generate an API key used for authorization.
appPackage/apiSpecificationFile/repair.yml
A file that describes the structure and behavior of the repair API.
appPackage/responseTemplates/repair.json
A template file for rendering API response.
teamsapp.yml
The main Teams Toolkit project file. The project file defines two primary things: Properties and configuration Stage definitions.
teamsapp.local.yml
Overrides teamsapp.yml with actions that enable local execution and debugging.
aad.manifest.json
Defines the configuration of Microsoft Entra app. This template only provisions a single tenant Microsoft Entra app.
Based on the options selected in step a, follow these steps:
If you've selected none or Microsoft Entra, skip to the next step.
If you've selected API key, follow these steps:
Generate and set up your API key as follows:
In Visual Studio Code, go to View > Terminal.
Run the following command to install dependency packages:
npm install
Run the following command to generate your API key:
npm run keygen
The API key is generated as Generated a new API Key: xxx.... The generated API key is registered and recorded in the API key registration tool in Developer portal for Teams. For more information on API key registration, see Register an API key.
Enter the generated API key into your env/.env.*.user file. Replace <your-api-key> with the actual key:
SECRET_API_KEY=<your-api-key>
Enter or browse the OpenAPI Description document location.
From the API list, select the required APIs and select OK.
Note
GET and POST APIs are supported for API-based message extensions.
Select Default folder.
Enter the name of your app and select Enter. Teams Toolkit scaffolds the OpenAPI Description document and created an API-based message extension.
Under LIFECYCLE, select Provision.
If your OpenAPI specification document has a security scheme bearerAuth, which uses the HTTP bearer scheme, enter the API key in the command window and select Enter.
Note
The API key must be a string with 10 to 128 characters.
Note
Teams toolkit source file includes a security check to ensure that an incoming request is authorized. It uses a function isApiKeyValid(req) to verify if the request contains a valid API key. If the API key isn't valid, the code returns an 401 HTTP status code, indicating an Unauthorized response.
From the left pane, select Teams Toolkit.
Under ACCOUNTS, sign in with your Microsoft 365 account and Azure account if you haven't already.
From the left pane, Select Run and Debug (Ctrl+Shift+D).
From the launch configuration dropdown, select Preview in Teams (Edge) or Preview in Teams (Chrome). Teams Toolkit launches Teams web client in a browser window.
Go to a chat message and select the Actions and apps icon. In the flyout menu, search for your app.
Select your message extension from the list and enter a search command in the search box.
Select an item from the list. The item unfurls into an Adaptive Card in the message compose area.
Select Send. Teams sends the search result as an Adaptive Card in the chat message.
To create an API-based message extension using Teams Toolkit CLI, follow these steps:
Go to Command Prompt.
Enter the following command:
npm install -g @microsoft/teamsapp-cli
Type teamsapp new in the terminal
Select Message Extension.
Select Custom Search Results.
Select Start from an OpenAPI Description Document.
Enter a valid URL or local path of your OpenAPI Description document.
Select the APIs from the list and select Enter.
Enter the location for your project and select Enter.
Enter the name of your application and select Enter.
Go to the folder path where your project is created and enter the following command to provision your app in Azure:
teamsapp provision --env dev
Teams Toolkit CLI opens a browser window and requests you to sign in to your Microsoft Account.
Sign in to your Microsoft account. Teams Toolkit CLI executes validation and provisions your app on Azure.
In the command prompt window, enter the following command to preview your app in Teams:
Preview the app: teamsapp preview --env dev
A new browser window with Teams web client opens. You can add your app to Teams.
Before you get started, ensure that you install Visual Studio Enterprise 2022 Preview version 17.9.0 Preview 1.0 and install the Microsoft Teams development tools under ASP.NET and web development workload.
To create an API-based message extension using Teams Toolkit for Visual Studio, follow these steps:
Open Visual Studio.
Go to File > New > Project... or New Project.
Search for Teams and select Microsoft Teams App.
Enter the Project name and Location.
Select Create.
Select Search Results from API.
Select any of the following options:
If you want to start without an API, select Start with a new API.
If you have an existing OpenAPI Description document, select Start with an OpenAPI Description.
Select Next.
Based on the options selected in step 7, select the following:
To get started, you must update the source code in the following files:
File
Contents
Repair.cs
The main file of a function in Azure Functions. Defines an Azure Function that retrieves and filters repair records based on a query parameter from an HTTP GET request, and returns the results as a JSON response.
RepairData.cs
The data source for the repair API. Contains a method that returns a hardcoded list of car repair tasks.
Models/RepairModel.cs
Defines a data model that represents a repair task with properties such as ID, Title, Description, AssignedTo, Date, and Image.
appPackage/apiSpecificationFile/repair.yml
A file that describes the structure and behavior of the repair API.
appPackage/responseTemplates/repair.json
A generated Adaptive Card that used to render API response.
appPackage/responseTemplates/repair.data.json
The data source for the repair API.
teamsapp.yml
The main Teams Toolkit project file. The project file defines two primary things: Properties and configuration Stage definitions.
teamsapp.local.yml
Overrides teamsapp.yml with actions that enable local execution and debugging.
After you've updated the source code, in the debug dropdown menu, select Dev Tunnels (no active tunnel) > Create a Tunnel....
Select an account to create the tunnel. The supported account types are Azure, Microsoft Account (MSA), and GitHub.
Name: Enter a name for the tunnel.
Tunnel Type: Select Persistent or Temporary.
Access: Select Public.
Select OK. Visual Studio displays a confirmation message that a tunnel is created.
The tunnel you created is listed under Dev Tunnels.
Go to Solution Explorer and select your project.
Right-click the menu and select Teams Toolkit > Prepare Teams App Dependencies.
If prompted, sign in with a Microsoft 365 account. A message appears that the app is successfully prepared.
Select the F5 key or select Debug > Start Debugging. Visual Studio launches a Teams web client.
Enter OpenAPI specification URL or select Browse.. to upload a file from your local machine.
Select the dropdown and select the APIs from the list.
Select Create. The project is scaffolded and you can find API specification, manifest, and response template files in the appPackage folder.
Go to Solution Explorer and select your project.
Right-click the menu and select Teams Toolkit > Provision in the Cloud.
If prompted, sign in with a Microsoft 365 account. A message appears that the app is successfully prepared.
Right-click your project and select Teams Toolkit > Preview in > Teams.
Select the manifest.json file and select Open. Visual Studio launches a Teams web client.
Go to a chat and select Actions and apps.
From the message extension fly-out menu, enter the name of your message extension in the search box.
Select the message extension and enter your search query.
Select an item from the list. The item unfurls into an Adaptive Card in the message compose area.
Select Send. Teams sends the search result as an Adaptive Card in the chat message.
Step-by-step guides
To build an API-based message extension, follow these step-by-step guides:
For beginners: Build an API-based message extension using Teams Toolkit.
For advanced users: Build an API-based message extension from the ground up.
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.