The @microsoft/agents-copilotstudio-client package allows you to interact with Copilot Studio Agents using the Direct Engine Protocol. This client library is designed to facilitate communication with agents, enabling seamless integration and interaction within your JavaScript or TypeScript applications.
This package provides exports for CommonJS and ES6 modules, and also a bundle to be used in the browser.
The Client needs to be initialized with a valid JWT Token.
To install the package, use npm or yarn:
npm install @microsoft/agents-copilotstudio-client
To use this library, you will need the following:
If you are using this client from a service, you will need to exchange the user token used to login to your service for a token for your agent hosted in copilot studio. This is called a On Behalf Of (OBO) authentication token. You can find more information about this authentication flow in Entra Documentation.
When using this method, you will need to add the CopilotStudio.Copilots.Invoke delegated API permision to your application registration's API privilages
This step will require permissions to edit application identities in your Azure tenant.
API's my organization usesPower Platform API.
Power Platform API see the note at the bottom of this section.Delegated Permissions
CopilotStudio and Check CopilotStudio.Copilots.InvokeAdd PermissionsApplication Permissions
CopilotStudio and Check CopilotStudio.Copilots.InvokeAdd PermissionsGrant Admin consent for copilotsdk before the permissions will be available to the application.
If you do not see Power Platform API in the list of API's your organization uses, you need to add the Power Platform API to your tenant. To do that, goto Power Platform API Authentication and follow the instructions on Step 2 to add the Power Platform Admin API to your Tenant
The Copilot Client is configured using the ConnectionSettings class and a jwt token to authenticate to the service.
The ConnectionSettings class can be configured using either instantiating the class or loading the settings from a .env.
There are a few options for configuring the ConnectionSettings class. The following are the most common options:
Using Environment ID and Copilot Studio Agent Schema Name:
const settings: ConnectionSettings = {
environmentId: "your-environment-id",
agentIdentifier: "your-agent-schema-name",
};
Using the DirectConnectUrl:
const settings: ConnectionSettings = {
directConnectUrl: "https://direct.connect.url",
};
By default, it's assumed your agent is in the Microsoft Public Cloud. If you are using a different cloud, you will need to set the Cloud property to the appropriate value. See the PowerPlatformCloud enum for the supported values
You can use the loadCopilotStudioConnectionSettingsFromEnv function to load the ConnectionSettings from a .env file.
The following are the most common options:
Using Environment ID and Copilot Studio Agent Schema Name:
environmentId=your-environment-id
agentIdentifier=your-agent-schema-name
Using the DirectConnectUrl:
directConnectUrl=https://direct.connect.url
const createClient = async (): Promise<CopilotStudioClient> => {
const settings = loadCopilotStudioConnectionSettingsFromEnv()
const token = await acquireToken(settings)
const copilotClient = new CopilotStudioClient(settings, token)
return copilotClient
}
const copilotClient = await createClient()
const replies = await copilotClient.startConversationAsync(true)
replies.forEach(r => console.log(r.text))