# Designing a MailMerge Web API Endpoint with ASP.NET Core in C#

> This article shows how to create a Web API endpoint that merges templates with JSON data using TX Text Control .NET Server Core. It shows how to design the endpoint to accept the template, the data, and settings for the merge process.

- **Author:** Bjoern Meyer
- **Published:** 2024-07-12
- **Modified:** 2026-07-17
- **Description:** This article shows how to create a Web API endpoint that merges templates with JSON data using TX Text Control .NET Server Core. It shows how to design the endpoint to accept the template, the data, and settings for the merge process.
- **7 min read** (1343 words)
- **Tags:**
  - ASP.NET
  - ASP.NET Core
  - MailMerge
  - Web API
- **Web URL:** https://www.textcontrol.com/blog/2024/07/12/designing-a-mailmerge-web-api-endpoint-with-asp-net-core-in-c-sharp/
- **LLMs URL:** https://www.textcontrol.com/blog/2024/07/12/designing-a-mailmerge-web-api-endpoint-with-asp-net-core-in-c-sharp/llms.txt
- **LLMs-Full URL:** https://www.textcontrol.com/blog/2024/07/12/designing-a-mailmerge-web-api-endpoint-with-asp-net-core-in-c-sharp/llms-full.txt
- **GitHub Repository:** https://github.com/TextControl/TXTextControl.MailMerge.WebAPI

---

The MailMerge class provides very powerful document generation capabilities based on a template and JSON data to create MS Word DOCX files or PDF documents. You can specify settings such as removing blank spaces or empty fields during the merge process. But how do you design a Web API with all these options, and how do you pass the template and data to the endpoint?

> **Learn More**
> 
> The MailMerge class provides very effective ways to merge data into MS Word compatible templates. This updated ultimate guide provides an overview of all the important features and functionalities of the mail merge process.
> 
> [An Ultimate Guide to Mail Merge with MS Word Documents in C# ](https://www.textcontrol.com/blog/2023/06/07/an-ultimate-guide-to-mail-merge-with-ms-word-documents-in-csharp/llms-full.txt)

### Designing the Web API

A typical merge process requires a template, data and settings for the MailMerge class, and additional settings for the generated PDF, such as author information.

Let's start with the Web API endpoint. The following code snippet shows the Web API endpoint that accepts a POST request with a JSON payload containing the template, data, and settings for the merge process.

```
public class DocumentController : Controller
{
  [HttpPost]
  public IActionResult Merge(
    [FromBody] MergeBody mergeBody)
  {

    #region Error Checking

    // error checking: MergeBody cannot be null
    if (mergeBody == null)
    {
      return BadRequest();
    }

    // error checking: Template must not be null
    if (mergeBody.Template == null)
    {
      return BadRequest("Template must not be null");
    }

    #endregion

    using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
    {
      tx.Create();

      // load the template
      try {         
        tx.Load(Convert.FromBase64String(mergeBody.Template), TXTextControl.BinaryStreamType.InternalUnicodeFormat);
      }
      catch (Exception ex) // format not valid
      {
        return BadRequest(ex.Message);
      }

      // create a new MailMerge object
      MailMerge mailMerge = new MailMerge()
      {
        TextComponent = tx,
        
        // set the merge settings
        DataSourceCulture = new CultureInfo(mergeBody.MergeSettings.DataSourceCulture),
        FormFieldMergeType = (FormFieldMergeType)mergeBody.MergeSettings.FormFieldMergeType,
        MergeCulture = new CultureInfo(mergeBody.MergeSettings.MergeCulture),
        RemoveEmptyBlocks = mergeBody.MergeSettings.RemoveEmptyBlocks ?? false,
        RemoveEmptyFields = mergeBody.MergeSettings.RemoveEmptyFields ?? false,
        RemoveEmptyImages = mergeBody.MergeSettings.RemoveEmptyImages ?? false,
        RemoveEmptyLines = mergeBody.MergeSettings.RemoveEmptyLines ?? false,
        RemoveTrailingWhitespace = mergeBody.MergeSettings.RemoveTrailingWhitespace ?? false,
      };

      // merge the data
      mailMerge.MergeJsonData(mergeBody.MergeData, true);

      // save settings
      TXTextControl.SaveSettings saveSettings = new TXTextControl.SaveSettings()
      {
        Author = mergeBody.MergeSettings.Author,
        CreationDate = (DateTime)mergeBody.MergeSettings.CreationDate,
        CreatorApplication = mergeBody.MergeSettings.CreatorApplication,
        DocumentSubject = mergeBody.MergeSettings.DocumentSubject,
        DocumentTitle = mergeBody.MergeSettings.DocumentTitle,
        LastModificationDate = (DateTime)mergeBody.MergeSettings.LastModificationDate,
        UserPassword = mergeBody.MergeSettings.UserPassword
      };

      byte[] results = null;

      // save the document as PDF
      tx.Save(out results, TXTextControl.BinaryStreamType.AdobePDF, saveSettings);

      // return the merged document as base64 string
      return Ok(Convert.ToBase64String(results));
    }
  }
}
```

First, we need to do some error handling by checking that there is a MergeBody object and that it has a template and data provided to it. If not, we return a 400 Bad Request response.

Then a ServerTextControl object is created and the template is loaded. The *MailMerge* class is initialized with the given settings and the merge process is started. The resulting document is saved as a PDF document with the given settings and returned as a base64-encoded string.

#### The MergeBody Class

The *MergeBody* class is a simple class that contains the template, data, and settings for the merge process. The following code snippet shows the *MergeBody* class, the *MergeSettings* class, and the *DocumentSettings* class.

```
using Microsoft.AspNetCore.Mvc;

namespace TXTextControl.DocumentProcessing.Models
{
  public class MergeBody
  {
    [BindProperty(Name = "mergeData")]
    public string MergeData { get; set; }

    [BindProperty(Name = "template")]
    public string Template { get; set; }

    [BindProperty(Name = "mergeSettings")]
    public MergeSettings MergeSettings { get; set; }
  }

  public class DocumentSettings
  {
    [BindProperty(Name = "author")]
    public string Author { get; set; }

    [BindProperty(Name = "creationDate")]
    public DateTime? CreationDate { get; set; }

    [BindProperty(Name = "creatorApplication")]
    public string CreatorApplication { get; set; }

    [BindProperty(Name = "documentSubject")]
    public string DocumentSubject { get; set; }

    [BindProperty(Name = "documentTitle")]
    public string DocumentTitle { get; set; }

    [BindProperty(Name = "lastModificationDate")]
    public DateTime? LastModificationDate { get; set; }

    [BindProperty(Name = "userPassword")]
    public string UserPassword { get; set; }
  }

  public class MergeSettings : DocumentSettings
  {
    [BindProperty(Name = "removeEmptyFields")]
    public bool? RemoveEmptyFields { get; set; }

    [BindProperty(Name = "removeEmptyBlocks")]
    public bool? RemoveEmptyBlocks { get; set; }

    [BindProperty(Name = "removeEmptyImages")]
    public bool? RemoveEmptyImages { get; set; }

    [BindProperty(Name = "removeTrailingWhitespace")]
    public bool? RemoveTrailingWhitespace { get; set; }

    [BindProperty(Name = "removeEmptyLines")]
    public bool? RemoveEmptyLines { get; set; }

    [BindProperty(Name = "formFieldMergeType")]
    public int? FormFieldMergeType { get; set; }

    [BindProperty(Name = "culture")]
    public string Culture { get; set; }

    [BindProperty(Name = "dataSourceCulture")]
    public string DataSourceCulture { get; set; }

    [BindProperty(Name = "mergeCulture")]
    public string MergeCulture { get; set; }
  }
}
```

### Calling the Web API

Now that we have the Web API endpoint, we can call it from a client application. The following code snippet shows how to call the Web API endpoint using the *HttpClient* class:

```
textControl1.Save(out data, TXTextControl.BinaryStreamType.InternalUnicodeFormat);

var MergeBody = new TXTextControl.DocumentProcessing.Models.MergeBody()
{
  MergeData = JsonConvert.SerializeObject(customer),
  Template = Convert.ToBase64String(data),
  MergeSettings = new TXTextControl.DocumentProcessing.Models.MergeSettings()
  {
    Author = "John Doe",
    CreationDate = DateTime.Now,
    CreatorApplication = "TX Text Control",
    DocumentSubject = "Subject",
    DocumentTitle = "Title",
    LastModificationDate = DateTime.Now,
    UserPassword = "password",
    RemoveEmptyFields = true,
    RemoveEmptyBlocks = true,
    RemoveEmptyImages = true,
    RemoveTrailingWhitespace = true,
    RemoveEmptyLines = true,
    FormFieldMergeType = 1,
    Culture = "en-US",
    DataSourceCulture = "en-US",
    MergeCulture = "en-US"
  }
};
    
var client = new HttpClient();

// convert content to json with lower case properties
var content = new StringContent(JsonConvert.SerializeObject(MergeBody, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }), Encoding.UTF8, "application/json");
var response = client.PostAsync("https://localhost:7241/Document/Merge", content).Result;

// get the response as a string
var responseString = response.Content.ReadAsStringAsync().Result;
```

The *HttpClient* class is used to send a POST request to the Web API endpoint with the JSON payload containing the template, data, and settings for the merge process. The response is read as a base64-encoded string and saved as a PDF file.

Let's take a look at the JSON payload that is sent to the Web API endpoint:

```
{
   "mergeData":"{\"FirstName\":\"John\",\"LastName\":\"Doe\"}",
   "template":"CAcBAA4AAAAAAAAAAAAXAAIAqwBGAGkAcgBzAHQATgBhAG0AZQC7AHQAZQB4AHQAQwBvAG4AdAByAG8AbAAxAAAANgIAAAMAAQABAAEAAAAAAAAAAgCfhwEAAQALAAAAAAAAQAEAkgcAAABQAQAMAAAAAAAAAABAAAAAAAAAAFABAAwADAAAAAAAAEA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AAAAAAAAAAAAAAAAAAAAAAAAAAAACQRkAAAAWwBOAG8AcgBtAGEAbABdAAAAUwB5AG0AYgBvAGwAAAAAAABAIAABAAMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQB6AAAAMgAAAABAAAAAADcCNwI3AjcC0C8AAOA9AACgBaAFoAWgBSAKP4AAAAAAAAAAAAEAAAAAAAAAGwEAAAAAAAAAAAAAAAAAAAAAAAAsAAAAAAAAAAAAxgHGAcYBxgEAAABAAAAAQAAAAEAAAABAAAAAAAAAAAAAAA==",
   "mergeSettings":{
      "removeEmptyFields":true,
      "removeEmptyBlocks":true,
      "removeEmptyImages":true,
      "removeTrailingWhitespace":true,
      "removeEmptyLines":true,
      "formFieldMergeType":1,
      "culture":"en-US",
      "dataSourceCulture":"en-US",
      "mergeCulture":"en-US",
      "author":"John Doe",
      "creationDate":"2024-07-12T14:44:27.7222043+02:00",
      "creatorApplication":"TX Text Control",
      "documentSubject":"Subject",
      "documentTitle":"Title",
      "lastModificationDate":"2024-07-12T14:44:27.7244266+02:00",
      "userPassword":"password"
   }
}
```

### Considerations

When designing a Web API for document generation, there are a few things to consider:

- **Security**  
    Ensure that authentication and authorization mechanisms are in place to secure the Web API endpoint.
- **Validation**  
    Prevent injection attacks and other security vulnerabilities by validating input data.
- **Performance**  
    To improve the responsiveness of the Web API, consider using a queuing system to offload the document generation process to a background task.
- **Scalability**  
    Use distributed architecture and load balancing to make the Web API scalable.
- **Monitoring**  
    Monitor the Web API for performance and availability using tools such as *Application Insights*.

#### Performance Considerations

Performance is an important consideration when generating large documents on the server side. You may want to consider using a queueing system to offload the document generation process to a background task to improve the responsiveness of the Web API. Depending on template size and data structure, document generation can be a complex, time-consuming task.

When generating documents with many nested merge blocks and 100s or 1000s of pages, this task can take several seconds or even minutes. A typical HTTP request should be returned within milliseconds with no significant delay. We have published a sample that shows how to queue requests and implement a REST web API that calls a web hook after the document has been successfully generated.

> **Learn More**
> 
> Depending on the template size and data structure, generating documents can be a complex, time-consuming task. This sample shows how to build an asynchronous, RESTful Web API that calls a WebHook after the document generation process is terminated.
> 
> [Generating Documents using a RESTful, Asynchronous Web API using WebHooks ](https://www.textcontrol.com/blog/2022/08/22/generating-documents-using-a-restful-asynchronous-web-api-using-webhooks/llms-full.txt)

### Conclusion

In this article, we have seen how to design a Web API for document generation using the *MailMerge* class. We have also seen how to call the Web API from a client application and some considerations to keep in mind when designing a Web API for document generation.

By following these best practices, you can create a robust and scalable Web API for document generation that meets the needs of your application.

Download the sample project from our GitHub repository and try it out for yourself!

---

## About Bjoern Meyer

As CEO, Bjoern is the visionary behind our strategic direction and business development, bridging the gap between our customers and engineering teams. His deep passion for coding and web technologies drives the creation of innovative products. If you're at a tech conference, be sure to stop by our booth - you'll most likely meet Bjoern in person. With an advanced graduate degree (Dipl. Inf.) in Computer Science, specializing in AI, from the University of Bremen, Bjoern brings significant expertise to his role. In his spare time, Bjoern enjoys running, paragliding, mountain biking, and playing the piano.

- [LinkedIn](https://www.linkedin.com/in/bjoernmeyer/)
- [X](https://x.com/txbjoern)
- [GitHub](https://github.com/bjoerntx)

---

## Related Posts

- [Building a Production-Ready PDF Generation Pipeline in C# with ASP.NET Core](https://www.textcontrol.com/blog/2026/10/12/reliable-pdf-generation-pipeline-csharp-aspnet-core/llms.txt)
- [Create SignFabric Envelopes from Mail Merge Templates in .NET C#](https://www.textcontrol.com/blog/2026/06/23/create-signfabric-envelopes-from-mail-merge-templates-using-dotnet-csharp/llms.txt)
- [Use MailMerge in .NET on Linux to Generate Pixel-Perfect PDFs from DOCX Templates](https://www.textcontrol.com/blog/2025/05/27/use-mailmerge-in-dotnet-on-linux-to-generate-pixel-perfect-pdfs-from-docx-templates/llms.txt)
- [Generating Dynamic NDAs Using TX Text Control MailMerge in C# .NET](https://www.textcontrol.com/blog/2025/04/08/generating-dynamic-ndas-using-tx-text-control-mailmerge-in-csharp-dotnet/llms.txt)
- [Designing a Maintainable PDF Generation Web API in ASP.NET Core (Linux) C# with Clean Architecture and TX Text Control](https://www.textcontrol.com/blog/2025/03/27/designing-a-maintainable-pdf-generation-web-api-in-asp-net-core-linux-c-sharp-with-clean-architecture-and-tx-text-control/llms.txt)
- [Manipulating Table Cells During the MailMerge Process in .NET C#](https://www.textcontrol.com/blog/2024/12/03/manipulating-table-cells-during-the-mailmerge-process-in-net-csharp/llms.txt)
- [When to Generate Documents Server-Side Instead of Client-Side: A Focus on Data Security](https://www.textcontrol.com/blog/2024/10/04/when-to-generate-documents-server-side-instead-of-client-side-a-focus-on-data-security/llms.txt)
- [Mail Merge: Inserting Merge Blocks using the DataSourceManager in C#](https://www.textcontrol.com/blog/2024/10/02/mail-merge-inserting-merge-blocks-using-the-datasourcemanager-in-csharp/llms.txt)
- [Creating Advanced Tables in PDF and DOCX Documents with C#](https://www.textcontrol.com/blog/2024/09/30/creating-advanced-tables-in-pdf-and-docx-documents-with-csharp/llms.txt)
- [Designing the Perfect Contract Template for MailMerge in C#](https://www.textcontrol.com/blog/2024/09/26/designing-the-perfect-contract-template-for-mailmerge-in-csharp/llms.txt)
- [Video Tutorial: Creating a MailMerge Template and JSON Data Structure](https://www.textcontrol.com/blog/2024/08/16/video-tutorial-creating-a-mailmerge-template-and-json-data-structure/llms.txt)
- [Getting Started Video Tutorial: How to use the MailMerge and ServerTextControl Classes in ASP.NET Core C#](https://www.textcontrol.com/blog/2024/08/05/getting-started-video-tutorial-how-to-use-the-mailmerge-and-servertextcontrol-classes-in-asp-net-core-c/llms.txt)
- [Getting Started Videos: New Text Control YouTube Channel](https://www.textcontrol.com/blog/2024/08/02/getting-started-videos-new-text-control-youtube-channel/llms.txt)
- [Best Practices for Mail Merge and Form Field Processing in ASP.NET Core C# Applications](https://www.textcontrol.com/blog/2024/07/30/best-practices-for-mail-merge-and-form-field-processing-in-asp-net-core-csharp-applications/llms.txt)
- [Advantages of Flow Type Layout Reporting vs. Banded Reporting or PDF Template Engines in .NET C#](https://www.textcontrol.com/blog/2024/07/29/advantages-of-flow-type-layout-reporting-vs-banded-reporting-or-pdf-template-engines-in-net-c-sharp/llms.txt)
- [Enhancing Documents with QR Codes and Barcodes in .NET C#: A Comprehensive Guide](https://www.textcontrol.com/blog/2024/07/11/enhancing-documents-with-qr-codes-and-barcodes-in-net-csharp-a-comprehensive-guide/llms.txt)
- [Document Automation 101: Leveraging TX Text Control for Business Efficiency in .NET C# Applications](https://www.textcontrol.com/blog/2024/07/09/document-automation-101-leveraging-tx-text-control-for-business-efficiency-in-net-c-applications/llms.txt)
- [Merging Templates with MailMerge with Different Merge Field Settings in C#](https://www.textcontrol.com/blog/2023/12/16/merging-templates-with-mailmerge-with-different-merge-field-settings/llms.txt)
- [How to Mail Merge MS Word DOCX Documents in ASP.NET Core C#](https://www.textcontrol.com/blog/2023/10/16/how-to-mail-merge-ms-word-docx-documents-in-aspnet-core-csharp/llms.txt)
- [MailMerge: Working with Image Placeholders](https://www.textcontrol.com/blog/2022/12/22/mailmerge-working-with-image-placeholders/llms.txt)
- [Getting Started: ServerTextControl and MailMerge with ASP.NET Core](https://www.textcontrol.com/blog/2022/09/01/getting-started-servertextcontrol-and-mailmerge-with-aspnet-core/llms.txt)
- [Adding SVG Watermarks to Documents](https://www.textcontrol.com/blog/2022/01/28/adding-svg-watermarks-to-documents/llms.txt)
- [Using MailMerge in ASP.NET Core 6 Web Applications](https://www.textcontrol.com/blog/2022/01/27/using-mailmerge-in-aspnet-core-6-web-applications/llms.txt)
- [Building a Spell Checking Web API in ASP.NET Core](https://www.textcontrol.com/blog/2020/05/15/building-a-spell-checking-web-api-in-aspnet-core/llms.txt)
- [code.talks 2026: Two Fantastic Days of Developers, Innovation, and Great Conversations in Hamburg](https://www.textcontrol.com/blog/2026/11/06/code-talks-2026-hamburg/llms.txt)
