Create Barcodes in C# .NET

This C# sample shows how to create barcodes with the McAPI Barcode REST API service. The sample uses C# with .NET 5 but older .NET runtimes should work too.

Requirements: A RapidAPI account. Replace YOUR_API_KEY with your own api key.

The sample works with the free tier of the API, see RapidAPI McAPI Barcode Listing for available plans.

See the overview page for a reference that lists all available parameters and error codes.

Create a Barcode using C# with HttpClient

Shown is the creation of a Code 128 (code type: 7) with C#. All parameters relevant for the actual barcode creation are in the payload-string which is added as content to the request. The payload-string is formatted as a C# verbatim string for readability.

The data to encode in the barcode is "123456". The module width is set to 0.5mm, the height is set to 20mm, the size of the human readable text under the barcode is set to 10 points. The code is returned as a base64 encoded PDF.

The source code:

// Listing: Create barcodes in C#
  
using Newtonsoft.Json;
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace BarcodeAPI
{
    public class Result
    {
        public string service { get; set; }
        public string version { get; set; }
        public string format { get; set; }
        public string code { get; set; }
    }

    class Program
    {
        static async Task Main(string[] args)
        {

            string payload = @"
            {
                ""data"": ""123456"",
                ""type"": 7,
                ""moduleWidth"": 0.5,
                ""moduleHeight"": 20,
                ""hrText"": 10
                ""format"": ""pdf""
            }";

            using (var client = new HttpClient())
            {
                var request = new HttpRequestMessage
                {
                    Method = HttpMethod.Post,
                    RequestUri = new Uri("https://mcapi-barcode.p.rapidapi.com/"),
                    Headers =
                    {
                        { "x-rapidapi-key", "YOUR_API_KEY" },
                        { "x-rapidapi-host", "mcapi-barcode.p.rapidapi.com" },
                    },
                    Content = new StringContent(payload)
                    {
                        Headers =
                        {
                            ContentType = new MediaTypeHeaderValue("application/json")
                        }
                    }
                };

                using (var response = await client.SendAsync(request))
                {
                    response.EnsureSuccessStatusCode();
                    var body = await response.Content.ReadAsStringAsync();
                    ...
                }
            }
        }
    }
}

After performing the request, the response will be delivered as JSON in body, see further below for a snippet on how to parse the data:

{
  "service":"McAPI Barcode Generator, https://mcapi.io",
  "version":"V1",
  "format":"pdf",
  "code":"data:application/pdf;base64,JVBE ... Cg=="
}

McAPI Barcode API - Sample barcode with C#

The returned code from the C# snippet:

Image of Sample Barcode C#

This is just an introductory example. We provide more C# sample code for barcode creation here.