Integrate your .NET applications with DeepSeek

Recently, the Chinese start-up DeepSeek has gained prominence in the field of artificial intelligence with its revolutionary generative AI chatbot. DeepSeek’s model has achieved a level of performance comparable to giants like OpenAI and Anthropic, but at a significantly lower cost. In this article, we will explore how to integrate the DeepSeek API into a .NET application, showing step-by-step how to configure, send, and process messages to create an intelligent and fluid chat experience. If you want to add intelligent conversation functionalities to your project, this practical guide is the perfect starting point.

Creating an account on DeepSeek

First, go to the official DeepSeek website and fill out the form with your email and password.

After creating your account, log in and go to the API Keys section in the control panel and click on “Create new API Key”, copy and save the generated key.

Configuring the API key

Now, in the code we are going to write, you must enter your API key generated in DeepSeek. In the example below, the API key is stored directly in the apiKey variable. In real applications, avoid storing the key directly in the source code. Instead, you can store it in an environment variable or in a configuration file.

				
					public class Program
{
    private static readonly string apiKey = "Your-key";
    private static readonly string baseUrl = "https://api.deepseek.com/chat/completions";
    public  static async Task Main(string[] args)
    {
    //our code
    }
}
				
			

Main function for sending messages

In “Main,” we start by initializing the list of messages and configuring the system to send the messages to the DeepSeek API chat model. The API requires a specific data format for communication. Here, the code defines an entry point for the conversation, where the system sends an initial greeting message to DeepSeek:

				
					Console.WriteLine("Starting conversation with DeepSeek...");
var messages = new List<object>();
messages.Add(new { role = "system", content = "You are an intelligent and friendly assistant." });
				
			

This code simulates a conversation where “role” defines who is sending the message. “Content” stores the behavior of the assistant.

Reading user input and sending requests

Now we will create a “while” loop that will continue to request messages from the user and send them to DeepSeek. If the user wants to stop the “while” loop, they must send an empty message:

				
					while (true)
{
    //...processing code here
}
				
			

The next code blocks should be inserted inside the while(true) loop.

				
					Console.Write("You: ");
string userMessage = Console.ReadLine();
if (string.IsNullOrEmpty(userMessage))
    break;
				
			

After writing the message, we will add it to the list of messages:

				
					messages.Add(new { role = "user", content = userMessage });
				
			

Next, we will assemble the body of our request to send the request to DeepSeek. This request will contain all the messages exchanged up to that moment (user and assistant):

				
					var requestData = new
{
    model = "deepseek-chat",
    messages = messages.ToArray(),
    stream = false
};
				
			

Sending the HTTP request to the API

Now we will use the “HttpClient” class to send a POST request:

				
					using var httpClient = new HttpClient();
string jsonRequest = JsonSerializer.Serialize(requestData);
var content = new StringContent(jsonRequest, Encoding.UTF8, "application/json");
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
var response = await httpClient.PostAsync(baseUrl, content);
				
			

Here, the API key is passed in the request header through the “Authorization” header.

Handling the API response

After that, it is necessary to analyze the API response and extract the model’s response. The DeepSeek API returns the response in a JSON format, and we use “JsonDocument” to process it:

				
					var responseBody = await response.Content.ReadAsStringAsync();
using JsonDocument jsonDoc = JsonDocument.Parse(responseBody);
string contentText = jsonDoc
    .RootElement
    .GetProperty("choices")[0]
    .GetProperty("message")
    .GetProperty("content")
    .GetString();
				
			

The result will be only the model’s response without the other data.

Presenting the information and storing the new message

Finally, we can present the model’s response on the screen and add the received message to the list of messages, continuing in the ‘while’ loop:

				
					Console.WriteLine($"DeepSeek: {contentText}");
messages.Add(new { role = "assistant", content = contentText });
				
			

In the above image we can see a demonstration of the app:

Conclusion

In this article, we saw how to integrate with the DeepSeek API in a .NET application to create a simple chat application. Based on this code, you can expand the functionality to create graphical interfaces, store the message history in a database, or even integrate with other APIs or systems.