---
title: "How do I use AI APIs from C# and .NET?"  
description: "How do I use AI APIs from C# and .NET?"  
author: "Natalia Volkova"  
published: 2026-11-18  
updated: 2026-11-18  
canonical: https://answers.mindstick.com/qa/120207/how-do-i-use-ai-apis-from-c-and-net  
category: "AI for Developers & APIs"  
tags: ["c#", ".NET", "developers"]  
reading_time: 3 minutes  

---

# How do I use AI APIs from C# and .NET?



## Answers

### Answer by Ayesha Cheema

Python dominates AI tutorials, but .NET developers can build AI features in C# just as effectively. Whether you work with ASP.NET Core, Windows services, desktop apps or older ASP.NET applications on IIS, here are your options.

Official and supported SDKs:

1. OpenAI's official .NET library, available on NuGet, supports chat, the Responses API, embeddings, images, audio and more.\
2. Azure OpenAI is supported through Azure SDKs, useful for organisations using Microsoft Azure.\
3. Anthropic provides SDKs for several languages; check its documentation for current C# support. Claude models are also available through cloud platforms such as Amazon Bedrock, Google Cloud Vertex AI and Microsoft's AI platforms, each with .NET SDKs.\
4. Google provides SDKs for Gemini; check its documentation for .NET options.\
5. Microsoft.Extensions.AI provides common abstractions in .NET for working with different AI providers through a unified interface.\
6. Semantic Kernel is Microsoft's open-source framework for building AI applications and agents in .NET, with support for multiple providers, plugins and memory.

Calling the REST API directly is also simple. Every major AI API is an HTTPS endpoint that accepts JSON. With HttpClient, you can send a POST request with the required headers, such as the API key header, and a JSON body containing the model and messages, then parse the JSON response with System.Text.Json. This works even in older .NET Framework applications where newer SDKs may not be supported.

Practical tips for .NET applications:

1. Store API keys securely, using environment variables, user secrets during development, Azure Key Vault or encrypted configuration. Never put keys in web.config files committed to source control or in client-side JavaScript.\
2. Use HttpClient correctly. Reuse instances through IHttpClientFactory to avoid socket exhaustion.\
3. Set appropriate timeouts. AI responses can take longer than typical API calls, especially with reasoning models; adjust HttpClient timeouts and IIS request timeouts accordingly.\
4. Use streaming for chat interfaces, sending responses to the browser with server-sent events or SignalR.\
5. Use async and await throughout, to avoid blocking threads during long AI calls.\
6. Handle rate limits with retry policies, for example using Polly.\
7. Log token usage for cost tracking.\
8. Ensure TLS 1.2 or higher is enabled, which matters for older Windows Server and .NET Framework setups.

For Windows Server and IIS deployments, also check outbound firewall rules and proxy settings so your server can reach the AI provider's endpoints.


---

Original Source: https://answers.mindstick.com/qa/120207/how-do-i-use-ai-apis-from-c-and-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
