Today I’ll show you how to develop a Web API to Create vCard QR Codes using Azure Functions.

But wait what are Azure Functions?

As defined by Microsoft:

Azure Functions is a serverless event driven experience that extends the existing Azure App Service platform. These nano-services can scale based on demand and you pay only for the resources you consume.

And what does serverless means? I really like the definition given by Scot Hanselman in his post What is Serverless Computing? Exploring Azure Functions:

Serverless Computing is like this – Your code, a slider bar, and your credit card. You just have your function out there and it will scale as long as you can pay for it. It’s as close to"cloudy" as The Cloud can get.

Now let’s start:

Create a Function App


Head to portal.azure.com and hit the New button. Search for Function App and create one. You’ll be asked for an app name, resource group, app service plan and storage account where the code will live.

Create the function


Create a new Function, selecting the empty C# template and give it a name: (i.e QRCoder)

Add the code


Replace the contents of the Code (run.csx) section with the following code and save it:

 1#r "System.Drawing"
 2#r "QRCoder.dll"
 3
 4using System.Drawing;
 5using System.Drawing.Imaging;
 6using System.IO;
 7using System.Net;
 8using System.Net.Http;
 9using System.Net.Http.Headers;
10using QRCoder;
11
12public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
13{
14    // Read the json request
15    var qrRequest = await req.Content.ReadAsAsync<SimpleVCardRequest>();
16
17    // Create the vCard string
18    var vCard = "BEGIN:VCARD\n";
19    vCard += $"FN:{qrRequest.Name}\n";
20    vCard += $"TEL;WORK;VOICE:{qrRequest.Phone}\n";
21    vCard += "END:VCARD";
22
23    // Generate de QRCode
24    QRCodeGenerator qrGenerator = new QRCodeGenerator();
25    QRCodeData qrCodeData = qrGenerator.CreateQrCode(vCard, QRCodeGenerator.ECCLevel.Q);
26    QRCode qrCode = new QRCode(qrCodeData);
27
28    // Save the QRCode as a jpeg image and send it in the response.
29    using (Bitmap qrCodeImage = qrCode.GetGraphic(20))
30    using (MemoryStream ms = new MemoryStream())
31    {
32        qrCodeImage.Save(ms, ImageFormat.Jpeg);
33
34        var response = new HttpResponseMessage()
35        {
36            Content = new ByteArrayContent(ms.ToArray()),
37            StatusCode = HttpStatusCode.OK,
38        };
39        response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
40        return response;
41    }    
42}
43
44// Request class to hold the Name and Phone number used to generated the vCard QR Code
45public class SimpleVCardRequest
46{
47    public string Name { get; set; }
48    public string Phone { get; set; }
49}

Fixing the missing references

If you read the log you’ll notice the compiler can’t resolve the references to QRCoder and that’s because we are using a 3rd party library to generate the QR codes.

We need to upload the assemblies to the Functions App. Connect to your App via SFTP or your favorite method.

You’ll need to create a bin folder under the folder with your function’s name (i.e. QRCoder) and upload the QRCoder.dll and UnityEngine.dll files to it.

Create an HTML endpoint for the function


Head to the Integrate tab and add a new HTML trigger and save it with the default values.

Test the Web API


Head to the Develop tab and copy the Function Url:

<img src="/wp-content/uploads/2016/08/functionUrl.png" alt=“functionUrl” class=“aligncenter” size-medium wp-image-5662" srcset="/wp-content/uploads/2016/08/functionUrl.png 1800w, /wp-content/uploads/2016/08/functionUrl-300x28.png 300w, /wp-content/uploads/2016/08/functionUrl-768x70.png 768w, /wp-content/uploads/2016/08/functionUrl-1024x94.png 1024w, /wp-content/uploads/2016/08/functionUrl-250x23.png 250w" sizes="(max-width: 1800px) 100vw, 1800px" />

Now use postman to send payload to the Web API and get a QR Code

The response should show a working QR code like the following:

You can grab the code for the function here: https://gist.github.com/cmendible/4b6627bcf288b94af9be4d25f6e66d5f

Hope it helps!