Skip to main content

Functions

Agents may want to expose REST APIs that client applications can call. In SDK 2.1, implement these with standard ASP.NET endpoints (for example app.MapPost(...)) and protect them with authorization.

using Microsoft.Teams.Core.Hosting;

public class ProcessMessageData
{
public required string Message { get; set; }
}

var builder = WebApplication.CreateSlimBuilder(args);
builder.Services.AddBotAuthorization();

var app = builder.Build();
app.UseAuthentication();
app.UseAuthorization();

app.MapPost("/api/functions/process-message", (ProcessMessageData data, ILogger<Program> logger) =>
{
logger.LogInformation("process-message with: {Message}", data.Message);
return Results.Ok(new { success = true });
})
.RequireAuthorization();

In SDK 2.1, token validation is handled by ASP.NET auth middleware plus .RequireAuthorization().

warning

This SDK does not validate that function arguments are of the expected types or otherwise trustworthy. Always validate request payloads before using them.

In SDK 2.1, return standard ASP.NET minimal API results:

app.MapPost("/api/functions/get-random-number", () =>
{
return Results.Ok(new { value = 4 });
})
.RequireAuthorization();

Function context

SDK 2.1 function endpoints use standard ASP.NET handler parameters instead of a Teams FunctionContext.

Parameter / SourceDescription
ProcessMessageData dataRequest body payload from caller
ClaimsPrincipal userAuthenticated user claims
HttpContextRequest context (headers, route data, services)
ILogger<T>Logging from DI
Other DI servicesAny registered service needed by your endpoint

In SDK 2.1, use .RequireAuthorization() so only authenticated callers reach the endpoint. You should still validate caller payload, route values, and business-level authorization.

warning

Authentication is not authorization for specific business resources. Always enforce resource-level checks in your handler.

In SDK 2.1, implement helper behavior explicitly in your endpoint (for example, resolve target conversation, verify access, then send via ConversationClient).

Additional resources

  • For the SDK 2.1 route-based example, see core/samples/TabApp/Program.cs in the Teams .NET repository.
  • For details on how tab apps invoke these functions, see the TypeScript Executing Functions in-depth guide.