-
Notifications
You must be signed in to change notification settings - Fork 199
Integrate API Connection with your Teams app
Teams Toolkit helps you to access existing APIs for building Teams applications. These APIs are developed by your organization or third-party.
Make sure that your project contains backend service, such as Azure Function or Azure bot service.
The following steps help you to add API connection manually:
- Step 1: Add SDK to project
- Step 2: Provide ApiClient for project
- Step 3: Add Configuration for local debugging
Add a reference to the @microsoft/teamsfx
package to package.json
.
Create a module to connect your API, and export the apiClient
to provide for the project.
Basic Auth
Sample code for Basic Auth
const teamsfxSdk = require("@microsoft/teamsfx");
// Initialize a new axios instance to call your API
const authProvider = new teamsfxSdk.BasicAuthProvider(
process.env.TEAMSFX_API_USERNAME,
process.env.TEAMSFX_API_PASSWORD
);
const apiClient = teamsfxSdk.createApiClient(
process.env.TEAMSFX_API_ENDPOINT,
authProvider
);
module.exports.apiClient = apiClient;
Certification
Sample code for Certification
const teamsfxSdk = require("@microsoft/teamsfx");
// Initialize a new axios instance to call your API
const authProvider = new teamsfxSdk.CertificateAuthProvider(
// TODO:
// 1. Add code to read your certificate and private key.
// 2. Replace "<your-cert>" and "<your-private-key>" with your actual certificate and private key values
// If you have a .pfx certificate, you can use the `createPfxCertOption` function to initialize your certificate
teamsfxSdk.createPemCertOption("<your-cert>", "<your-private-key>")
);
const apiClient = teamsfxSdk.createApiClient(
process.env.TEAMSFX_API_ENDPOINT,
authProvider
);
module.exports.apiClient = apiClient;
Azure Active Directory
There are 2 scenarios here, please choose one of them.
- Scenario 1 is reusing the project AAD app, make sure your project contains an existing AAD app.
- Scenario 2 is using an existing AAD App.
const teamsfxSdk = require("@microsoft/teamsfx");
// There are 2 scenarios here, please choose one of them. This sample uses the client credential flow to acquire a token for your API.
// Scenario 1. reuse the project AAD app.
const appAuthConfig: AppCredentialAuthConfig = {
authorityHost: process.env.AAD_APP_OAUTH_AUTHORITY_HOST,
clientId: process.env.TEAMSFX_API_CLIENT_ID,
tenantId: process.env.TEAMSFX_API_TENANT_ID,
clientSecret: process.env.TEAMSFX_API_CLIENT_SECRET,
};
// Scenario 2. use an existing AAD App.
const appAuthConfig: AppCredentialAuthConfig = {
authorityHost: "https://login.microsoftonline.com",
clientId: process.env.TEAMSFX_API_CLIENT_ID,
tenantId: process.env.TEAMSFX_API_TENANT_ID,
clientSecret: process.env.TEAMSFX_API_CLIENT_SECRET,
};
const appCredential = new AppCredential(appAuthConfig);
// Initialize a new axios instance to call your API
const authProvider = new teamsfxSdk.BearerTokenAuthProvider(
// TODO: Replace '<your-api-scope>' with your required API scope
async () => (await appCredential.getToken("<your-api-scope>")).token
);
const apiClient= teamsfxSdk.createApiClient(
process.env.TEAMSFX_API_ENDPOINT,
authProvider
);
module.exports.apiClient= apiClient;
API Key
Sample code for API Key
const teamsfxSdk = require("@microsoft/teamsfx");
// Initialize a new axios instance to call kudos, store API key in request header.
const authProvider = new teamsfxSdk.ApiKeyProvider(
"{API-KEY-name}",
process.env.TEAMSFX_API_API_KEY,
teamsfxSdk.ApiKeyLocation.Header
);
// or store API key in request params.
const authProvider = new teamsfxSdk.ApiKeyProvider(
"{API-KEY-name}",
process.env.TEAMSFX_API_API_KEY,
teamsfxSdk.ApiKeyLocation.QueryParams
);
const apiClient = teamsfxSdk.createApiClient(
process.env.TEAMSFX_API_ENDPOINT,
authProvider
);
module.exports.apiClient = apiClient;
Custom Auth Implementation
Sample code for Custom Auth Implementation
const teamsfxSdk = require("@microsoft/teamsfx");
// A custom authProvider implements the `AuthProvider` interface.
// This sample authProvider implementation will set a custom property in the request header
class CustomAuthProvider {
customProperty;
customValue;
constructor(customProperty, customValue) {
this.customProperty = customProperty;
this.customValue = customValue;
}
// Replace the sample code with your own logic.
AddAuthenticationInfo = async (config) => {
if (!config.headers) {
config.headers = {};
}
config.headers[this.customProperty] = this.customValue;
return config;
};
}
const authProvider = new CustomAuthProvider(
// You can also add configuration to the file `.env.teamsfx.local` and use `process.env.{setting_name}` to read the configuration. For example:
// process.env.TEAMSFX_API_CUSTOM_PROPERTY,
// process.env.TEAMSFX_API_CUSTOM_VALUE
"customPropery",
"customValue"
);
// Initialize a new axios instance to call your API
const apiClient = teamsfxSdk.createApiClient(
process.env.TEAMSFX_API_ENDPOINT,
authProvider
);
module.exports.apiClient = apiClient;
You can import the apiClient
(an Axios instance) in another file and call the APIs and authentication is now handled for you automatically.
Here is an example for a GET request to "relative_path_of_target_api":
const { apiClient } = require("relative_path_to_this_file");
const response = await apiClient.get("relative_path_of_target_api");
// You only need to enter the relative path for your API.
// For example, if you want to call api https://my-api-endpoint/test and you configured https://my-api-endpoint as the API endpoint,
// your code will be: const response = await kudosClient.get("test");
const responseBody = response.data;
Add configuration of your API in teamsfx/script/run.js
for local debugging.
Basic Auth
Add your Api connection configuration to teamsfx/script/run.js
...
const envs = await utils.loadEnv(args[0], args[1]);
// set up environment variables required by teamsfx
process.env.TEAMSFX_API_ENDPOINT =
process.env.TEAMSFX_API_USERNAME =
process.env.TEAMSFX_API_PASSWORD =
Certification
Add your Api connection configuration to teamsfx/script/run.js
...
const envs = await utils.loadEnv(args[0], args[1]);
// set up environment variables required by teamsfx
process.env.TEAMSFX_API_ENDPOINT =
Azure Active Directory
There are 2 scenarios here, please choose one of them.
- Scenario 1 is reusing the project AAD app, make sure your project contains an existing AAD app.
- Scenario 2 is using an existing AAD App.
Add your Api connection configuration to teamsfx/script/run.js
...
const envs = await utils.loadEnv(args[0], args[1]);
// set up environment variables required by teamsfx
process.env.TEAMSFX_API_ENDPOINT =
// Scenario 1
process.env.TEAMSFX_API_TENANT_ID = envs.AAD_APP_TENANT_ID
process.env.TEAMSFX_API_CLIENT_ID = envs.AAD_APP_CLIENT_ID
process.env.TEAMSFX_API_CLIENT_SECRET = envs.SECRET_AAD_APP_CLIENT_SECRET
process.env.AAD_APP_OAUTH_AUTHORITY_HOST = envs.AAD_APP_OAUTH_AUTHORITY_HOST
// Scenario 2
process.env.TEAMSFX_API_TENANT_ID =
process.env.TEAMSFX_API_CLIENT_ID =
process.env.TEAMSFX_API_CLIENT_SECRET =
API Key
Add your Api connection configuration to teamsfx/script/run.js
...
const envs = await utils.loadEnv(args[0], args[1]);
// set up environment variables required by teamsfx
process.env.TEAMSFX_API_ENDPOINT =
process.env.TEAMSFX_API_API_KEY =
Custom Auth Implementation
Add your Api connection configuration to teamsfx/script/run.js
...
const envs = await utils.loadEnv(args[0], args[1]);
// set up environment variables required by teamsfx
process.env.TEAMSFX_API_ENDPOINT=
Build Custom Engine Copilots
- Build a basic AI chatbot for Teams
- Build an AI agent chatbot for Teams
- Expand AI bot's knowledge with your content
Scenario-based Tutorials
- Send notifications to Teams
- Respond to chat commands in Teams
- Respond to card actions in Teams
- Embed a dashboard canvas in Teams
Extend your app across Microsoft 365
- Teams tabs in Microsoft 365 and Outlook
- Teams message extension for Outlook
- Add Outlook Add-in to a Teams app
App settings and Microsoft Entra Apps
- Manage Application settings with Teams Toolkit
- Manage Microsoft Entra Application Registration with Teams Toolkit
- Use an existing Microsoft Entra app
- Use a multi-tenant Microsoft Entra app
Configure multiple capabilities
- How to configure Tab capability within your Teams app
- How to configure Bot capability within your Teams app
- How to configure Message Extension capability within your Teams app
Add Authentication to your app
- How to add single sign on in Teams Toolkit for Visual Studio Code
- How to enable Single Sign-on in Teams Toolkit for Visual Studio
Connect to cloud resources
- How to integrate Azure Functions with your Teams app
- How to integrate Azure API Management
- Integrate with Azure SQL Database
- Integrate with Azure Key Vault
Deploy apps to production