1
---
2
name: typespec-create-api-plugin
3
description: 'Generate a TypeSpec API plugin with REST operations, authentication, and Adaptive Cards for Microsoft 365 Copilot'
4
---
5
6
# Create TypeSpec API Plugin
7
8
Create a complete TypeSpec API plugin for Microsoft 365 Copilot that integrates with external REST APIs.
9
10
## Requirements
11
12
Generate TypeSpec files with:
13
14
### main.tsp - Agent Definition
15
```typescript
16
import "@typespec/http";
17
import "@typespec/openapi3";
18
import "@microsoft/typespec-m365-copilot";
19
import "./actions.tsp";
20
21
using TypeSpec.Http;
22
using TypeSpec.M365.Copilot.Agents;
23
using TypeSpec.M365.Copilot.Actions;
24
25
@agent({
26
name: "[Agent Name]",
27
description: "[Description]"
28
})
29
@instructions("""
30
[Instructions for using the API operations]
31
""")
32
namespace [AgentName] {
33
// Reference operations from actions.tsp
34
op operation1 is [APINamespace].operationName;
35
}
36
```
37
38
### actions.tsp - API Operations
39
```typescript
40
import "@typespec/http";
41
import "@microsoft/typespec-m365-copilot";
42
43
using TypeSpec.Http;
44
using TypeSpec.M365.Copilot.Actions;
45
46
@service
47
@actions(#{
48
nameForHuman: "[API Display Name]",
49
descriptionForModel: "[Model description]",
50
descriptionForHuman: "[User description]"
51
})
52
@server("[API_BASE_URL]", "[API Name]")
53
@useAuth([AuthType]) // Optional
54
namespace [APINamespace] {
55
56
@route("[/path]")
57
@get
58
@action
59
op operationName(
60
@path param1: string,
61
@query param2?: string
62
): ResponseModel;
63
64
model ResponseModel {
65
// Response structure
66
}
67
}
68
```
69
70
## Authentication Options
71
72
Choose based on API requirements:
73
74
1. **No Authentication** (Public APIs)
75
```typescript
76
// No @useAuth decorator needed
77
```
78
79
2. **API Key**
80
```typescript
81
@useAuth(ApiKeyAuth<ApiKeyLocation.header, "X-API-Key">)
82
```
83
84
3. **OAuth2**
85
```typescript
86
@useAuth(OAuth2Auth<[{
87
type: OAuth2FlowType.authorizationCode;
88
authorizationUrl: "https://oauth.example.com/authorize";
89
tokenUrl: "https://oauth.example.com/token";
90
refreshUrl: "https://oauth.example.com/token";
91
scopes: ["read", "write"];
92
}]>)
93
```
94
95
4. **Registered Auth Reference**
96
```typescript
97
@useAuth(Auth)
98
99
@authReferenceId("registration-id-here")
100
model Auth is ApiKeyAuth<ApiKeyLocation.header, "X-API-Key">
101
```
102
103
## Function Capabilities
104
105
### Confirmation Dialog
106
```typescript
107
@capabilities(#{
108
confirmation: #{
109
type: "AdaptiveCard",
110
title: "Confirm Action",
111
body: """
112
Are you sure you want to perform this action?
113
* **Parameter**: {{ function.parameters.paramName }}
114
"""
115
}
116
})
117
```
118
119
### Adaptive Card Response
120
```typescript
121
@card(#{
122
dataPath: "$.items",
123
title: "$.title",
124
url: "$.link",
125
file: "cards/card.json"
126
})
127
```
128
129
### Reasoning & Response Instructions
130
```typescript
131
@reasoning("""
132
Consider user's context when calling this operation.
133
Prioritize recent items over older ones.
134
""")
135
@responding("""
136
Present results in a clear table format with columns: ID, Title, Status.
137
Include a summary count at the end.
138
""")
139
```
140
141
## Best Practices
142
143
1. **Operation Names**: Use clear, action-oriented names (listProjects, createTicket)
144
2. **Models**: Define TypeScript-like models for requests and responses
145
3. **HTTP Methods**: Use appropriate verbs (@get, @post, @patch, @delete)
146
4. **Paths**: Use RESTful path conventions with @route
147
5. **Parameters**: Use @path, @query, @header, @body appropriately
148
6. **Descriptions**: Provide clear descriptions for model understanding
149
7. **Confirmations**: Add for destructive operations (delete, update critical data)
150
8. **Cards**: Use for rich visual responses with multiple data items
151
152
## Workflow
153
154
Ask the user:
155
1. What is the API base URL and purpose?
156
2. What operations are needed (CRUD operations)?
157
3. What authentication method does the API use?
158
4. Should confirmations be required for any operations?
159
5. Do responses need Adaptive Cards?
160
161
Then generate:
162
- Complete `main.tsp` with agent definition
163
- Complete `actions.tsp` with API operations and models
164
- Optional `cards/card.json` if Adaptive Cards are needed
165