Create a complete TypeSpec API plugin for Microsoft 365 Copilot that integrates with external REST APIs.
Generate TypeSpec files with:
import "@typespec/http";
import "@typespec/openapi3";
import "@microsoft/typespec-m365-copilot";
import "./actions.tsp";
using TypeSpec.Http;
using TypeSpec.M365.Copilot.Agents;
using TypeSpec.M365.Copilot.Actions;
@agent({
name: "[Agent Name]",
description: "[Description]"
})
@instructions("""
[Instructions for using the API operations]
""")
namespace [AgentName] {
// Reference operations from actions.tsp
op operation1 is [APINamespace].operationName;
}
import "@typespec/http";
import "@microsoft/typespec-m365-copilot";
using TypeSpec.Http;
using TypeSpec.M365.Copilot.Actions;
@service
@actions(#{
nameForHuman: "[API Display Name]",
descriptionForModel: "[Model description]",
descriptionForHuman: "[User description]"
})
@server("[API_BASE_URL]", "[API Name]")
@useAuth([AuthType]) // Optional
namespace [APINamespace] {
@route("[/path]")
@get
@action
op operationName(
@path param1: string,
@query param2?: string
): ResponseModel;
model ResponseModel {
// Response structure
}
}
Choose based on API requirements:
-
No Authentication (Public APIs)
// No @useAuth decorator needed
-
API Key
@useAuth(ApiKeyAuth<ApiKeyLocation.header, "X-API-Key">)
-
OAuth2
@useAuth(OAuth2Auth<[{
type: OAuth2FlowType.authorizationCode;
authorizationUrl: "https://oauth.example.com/authorize";
tokenUrl: "https://oauth.example.com/token";
refreshUrl: "https://oauth.example.com/token";
scopes: ["read", "write"];
}]>)
The OAuth endpoints and scopes above are placeholders. Replace them with the real https endpoints of the provider, and request only the minimal scopes the selected operations need, confirmed with the user.
-
Registered Auth Reference
@useAuth(Auth)
@authReferenceId("registration-id-here")
model Auth is ApiKeyAuth<ApiKeyLocation.header, "X-API-Key">
Keep real API keys, client secrets, and tokens out of generated files and out of the chat. Prefer the registered auth reference option, and ask users to register credentials outside the plugin and share only the registration reference.
@capabilities(#{
confirmation: #{
type: "AdaptiveCard",
title: "Confirm Action",
body: """
Are you sure you want to perform this action?
* **Parameter**: {{ function.parameters.paramName }}
"""
}
})
@card(#{
dataPath: "$.items",
title: "$.title",
url: "$.link",
file: "cards/card.json"
})
In confirmation and response cards, keep the static action description separate from parameter values and show parameters as labeled values. Bind the card url only to fields the API guarantees are https links on the expected domain.
@reasoning("""
Consider user's context when calling this operation.
Prioritize recent items over older ones.
""")
@responding("""
Present results in a clear table format with columns: ID, Title, Status.
Include a summary count at the end.
""")
- Operation Names: Use clear, action-oriented names (listProjects, createTicket)
- Models: Define TypeScript-like models for requests and responses
- HTTP Methods: Use appropriate verbs (@get, @post, @patch, @delete)
- Paths: Use RESTful path conventions with @route
- Parameters: Use @path, @query, @header, @body appropriately
- Descriptions: Provide clear descriptions for model understanding
- Confirmations: Add by default for every operation that changes data (create, update, delete); leave it out only when the user explicitly opts out for that operation, and note the opt out in a comment
- Cards: Use for rich visual responses with multiple data items
- Dependencies: Pin exact versions of the TypeSpec libraries the plugin imports, and check package names carefully for lookalikes
Ask the user:
- What is the API base URL and purpose?
- What operations are needed (CRUD operations)?
- What authentication method does the API use?
- Should confirmations be required for any operations?
- Do responses need Adaptive Cards?
Require the base URL to use https. If it uses plain http, warn the user and ask again. Confirm the host is the intended API, and repeat the final URL back to the user before writing it into the server decorator.
Confirm the target directory first. If any of the files below already exist there, show the differences and ask before overwriting them.
Then generate:
- Complete
main.tsp with agent definition
- Complete
actions.tsp with API operations and models
- Optional
cards/card.json if Adaptive Cards are needed