Skip to content

tryAGI/DeepL

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

DeepL

Nuget package dotnet License: MIT Discord

Features 🔥

  • Fully generated C# SDK based on official DeepL OpenAPI specification using AutoSDK
  • Same day update to support new features
  • Updated and supported automatically if there are no breaking changes
  • All modern .NET features - nullability, trimming, NativeAOT, etc.
  • Support .Net Framework/.Net Standard 2.0

Usage

using DeepL;

using var client = new DeepLClient(apiKey);

Translate Text

Shows how to translate text between languages using the DeepL API.

using var client = new DeepLClient(apiKey);

// Translate text from English to German using the DeepL API.
var response = await client.TranslateText.TranslateTextAsync(
    request: new TranslateTextRequest
    {
        Text = ["Hello, world!"],
        TargetLang = TargetLanguage.De,
    });

Rephrase Text

Shows how to use the DeepL Write API to rephrase text.

using var client = new DeepLClient(apiKey);

// Use the DeepL Write API to rephrase text for improved style and clarity.
var response = await client.RephraseText.RephraseTextAsync(
    request: new RephraseTextRequest
    {
        Text = ["The weather is very nice today and I think we should go outside."],
        TargetLang = TargetLanguageWrite.EnUs,
    });

Get Usage

Shows how to retrieve account usage information.

using var client = new DeepLClient(apiKey);

// Retrieve your DeepL account usage (character count and limits).
var response = await client.MetaInformation.GetUsageAsync();

List Languages

Shows how to list supported languages.

using var client = new DeepLClient(apiKey);

// List all supported source and target languages.
var languages = await client.MetaInformation.GetLanguagesAsync();

Translate Document

Shows how to translate a document using the upload-poll-download workflow.

using var client = new DeepLClient(apiKey);

// DeepL's document translation follows a three-step workflow:
// 1. Upload the document and specify the target language
// 2. Poll for translation status until complete
// 3. Download the translated document

// Step 1: Upload a text file for translation.
var content = "Hello, world! This is a test document for translation."u8.ToArray();
var uploadResponse = await client.TranslateDocuments.TranslateDocumentAsync(
    targetLang: TargetLanguage.De,
    file: content,
    filename: "test.txt");

// Step 2: Poll until the document translation is complete.
GetDocumentStatusResponse status;
do
{
    await Task.Delay(1000);
    status = await client.TranslateDocuments.GetDocumentStatusAsync(
        documentId: uploadResponse.DocumentId!,
        documentKey1: uploadResponse.DocumentKey!);
}
while (status.Status is GetDocumentStatusResponseStatus.Queued
    or GetDocumentStatusResponseStatus.Translating);

// Step 3: Download the translated document (one-time download).
var translatedBytes = await client.TranslateDocuments.DownloadDocumentAsync(
    documentId: uploadResponse.DocumentId!,
    documentKey1: uploadResponse.DocumentKey!);

var translatedText = System.Text.Encoding.UTF8.GetString(translatedBytes);

Voice Streaming

Shows how to initiate a voice streaming session for real-time translation.

using var client = new DeepLClient(apiKey);

// The DeepL Voice API provides real-time speech translation via WebSocket.
// Step 1: Call the REST endpoint to get an ephemeral WebSocket URL and token.
var response = await client.VoiceAPI.GetVoiceStreamingUrlAsync(
    request: new GetVoiceStreamingUrlRequest
    {
        SourceMediaContentType = VoiceMediaContentType.AudioPcm_Encoding_s16le_Rate_16000,
        SourceLanguage = VoiceSourceLanguage.En,
        TargetLanguages = ["de"],
    });

// Step 2: Connect to the WebSocket URL (`response.StreamingUrl`)
// and stream audio bytes for real-time transcription and translation.
// This example only verifies the REST setup endpoint;
// actual WebSocket streaming requires a separate WebSocket client.

Style Rules

Demonstrates how to create, configure, and manage style rule lists for consistent translation output.

var client = new DeepLClient(apiKey);

// Style rules let you enforce terminology, formatting, and tone
// preferences across translations. They are configured per language.

// ## Create a style rule list

// Create a new style rule list for English with punctuation rules:
var created = await client.StyleRules.CreateStyleRuleListAsync(
    name: "SDK Test Rules",
    language: StyleRuleLanguage.En);

// ## List style rule lists

// Retrieve all style rule lists:
var lists = await client.StyleRules.GetStyleRuleListsAsync(detailed: true);

// ## Update configured rules

// Configure specific rule categories (7 available: DatesAndTimes,
// Formatting, Numbers, Punctuation, SpellingAndGrammar,
// StyleAndTone, Vocabulary):
var updated = await client.StyleRules.UpdateStyleRuleConfiguredRulesAsync(
    styleId: created.StyleId,
    punctuation: new ConfiguredRulesPunctuation());

// ## Clean up

await client.StyleRules.DeleteStyleRuleListAsync(
    styleId: created.StyleId);

Custom Instructions

Shows how to add free-text custom instructions to a style rule list for fine-grained control over translation output.

var client = new DeepLClient(apiKey);

// Custom instructions let you provide free-text rules that
// DeepL applies during translation — for example, enforcing
// terminology conventions or formatting preferences.

// ## Create a style rule list

var styleRule = await client.StyleRules.CreateStyleRuleListAsync(
    name: "Custom Instruction Demo",
    language: StyleRuleLanguage.En);

// ## Add a custom instruction

// Each instruction has a label and a prompt describing the rule:
var instruction = await client.StyleRules.CreateCustomInstructionAsync(
    styleId: styleRule.StyleId,
    label: "Currency formatting",
    prompt: "Always place the currency symbol before the number (e.g. $100, €50).");

// ## Retrieve the instruction

var retrieved = await client.StyleRules.GetCustomInstructionAsync(
    styleId: styleRule.StyleId,
    instructionId: instruction.Id);

// ## Add a source-language-specific instruction

// You can optionally restrict an instruction to a specific
// source language:
var deInstruction = await client.StyleRules.CreateCustomInstructionAsync(
    styleId: styleRule.StyleId,
    label: "German compound nouns",
    prompt: "Keep German compound nouns as a single word in the translation.",
    sourceLanguage: "de");

// ## Clean up

await client.StyleRules.DeleteCustomInstructionAsync(
    styleId: styleRule.StyleId,
    instructionId: deInstruction.Id);

await client.StyleRules.DeleteCustomInstructionAsync(
    styleId: styleRule.StyleId,
    instructionId: instruction.Id);

await client.StyleRules.DeleteStyleRuleListAsync(
    styleId: styleRule.StyleId);

Free API Endpoint

Shows how to use the DeepL Free API endpoint instead of the Pro endpoint.

// DeepL offers two API tiers with different base URLs:
// - Pro:  https://api.deepl.com (default)
// - Free: https://api-free.deepl.com
//
// To use the Free API, pass the base URL to the constructor.
using var freeClient = new DeepLClient(
    apiKey: "test-key",
    baseUri: new System.Uri("https://api-free.deepl.com"));

// The Pro API is the default — no base URL needed.
using var proClient = new DeepLClient(apiKey: "test-key");

Support

Priority place for bugs: https://github.com/tryAGI/DeepL/issues
Priority place for ideas and general questions: https://github.com/tryAGI/DeepL/discussions
Discord: https://discord.gg/Ca2xhfBf3v

Acknowledgments

JetBrains logo

This project is supported by JetBrains through the Open Source Support Program.

About

C# SDK for the DeepL API -- translation, text rephrasing, document translation, glossaries, style rules, and voice API

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors