Use DocsFold PDF generation API with C#

This code makes a POST request to the DocsFold API with the required headers and request body to generate a PDF file from the specified template and overrides.

You need to replace YOUR_API_KEY with your actual API key and YOUR_TEMPLATE_ID with your actual template ID.

This code example includes parsing the JSON response and downloading the PDF file from the specified URL. The PDF file is then saved to disk with the name "invoice.pdf".

Note that you'll need to adjust the file path as needed for the output PDF.

You'll also need to install the Newtonsoft.Json NuGet package to run this code.

using System;
using System.IO;
using System.Net;
using System.Text;
using Newtonsoft.Json.Linq;
class DocsFoldAPI
{
private const string API_KEY = "<YOUR_API_KEY_HERE>";
private const string API_URL = "https://app.docsfold.com/api/v1/generate-pdf";
private const string TEMPLATE_ID = "<YOUR_TEMPLATE_ID_HERE>";
static void Main(string[] args)
{
// JSON request body
JObject requestBody = new JObject(
new JProperty("template", TEMPLATE_ID),
new JProperty("output_format", "url"),
new JProperty("overrides", new JObject(
new JProperty("invoice_nr", "123"),
new JProperty("created_date", "January 1, 2022"),
new JProperty("due_date", "February 1, 2022"),
new JProperty("company_name", "Acme Corp"),
new JProperty("company_address", "Acme road, 1"),
new JProperty("company_postal_code", "Sunnyville, 1000"),
new JProperty("client_company_name", "Doe Corp"),
new JProperty("client_name", "John Doe"),
new JProperty("client_email", "john@doe.com"),
new JProperty("payment_method", "Check"),
new JProperty("payment_method_value", 1000),
new JProperty("items", new JArray(
new JObject(new JProperty("name", "Website design"), new JProperty("value", 300)),
new JObject(new JProperty("name", "Hosting (3 months)"), new JProperty("value", 75)),
new JObject(new JProperty("name", "Domain name (1 year)"), new JProperty("value", 10))
))
))
);
// Send the request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(API_URL);
request.Method = "POST";
request.Headers.Add("Authorization", "Bearer " + API_KEY);
request.ContentType = "application/json";
using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(requestBody.ToString());
}
// Get the response
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
string responseBody = reader.ReadToEnd();
JObject responseObject = JObject.Parse(responseBody);
string status = (string)responseObject["status"];
string pdfUrl = (string)responseObject["pdf_url"];
if (status == "SUCCESS" && !string.IsNullOrEmpty(pdfUrl))
{
// Download the PDF file and save it to disk
WebClient webClient = new WebClient();
byte[] pdfData = webClient.DownloadData(pdfUrl);
File.WriteAllBytes("invoice.pdf", pdfData);
Console.WriteLine("PDF file saved to invoice.pdf");
}
else
{
string message = (string)responseObject["message"];
Console.WriteLine("Error: " + message);
}
}
}
catch (WebException ex)
{
string message = ex.Message;
if (ex.Response != null)
{
using (StreamReader reader = new StreamReader(ex.Response.GetResponseStream(), Encoding.UTF8))
{
message = reader.ReadToEnd();
}
}
Console.WriteLine("Error: " + message);
}
}
}
Author Image
About the author

Gustavo Santos

Gustavo is the founder of DocsFold, based in Portugal. He has worked as a software developer for over 15 years. He enjoys exploring new technologies and using them to automate work as much as possible.