Generating barcodes and QR codes in Salesforce Marketing Cloud

QR codes and barcodes can make your subscriber’s life much easier and drive engagement with your platform. Barcodes are typically used for sending tickets, vouchers and discounts to your subscribers. Similarly, QR codes can also be used for sending tickets, etc., and allowing your subscribers to use their smartphones to easily access information or websites or personalized links. This short article will cover how to quickly generate them and include them in Salesforce Marketing Cloud emails and on CloudPages.

Generate Barcodes with AMPScript

AMPscript has a function dedicated solely to generating barcode image URLs – the BarcodeURL function. It can generate 25 kinds of barcodes, although 6 of them do not render correctly in Marketing Cloud (Mat25, OneCode, PlainText, Planet, Postnet and Telepen), hopefully, this will be addressed in the future. Here is a preview of the ones that are currently available for use:


The BarcodeURL function takes 9 arguments, 4 of them are required:

  • A value to convert into a barcode
  • Type of barcode to generate (as visible in the above screenshot)
  • Barcode image width in pixels
  • Barcode image height in pixels

Here is an example syntax, assuming that the values for our barcodes are stored in a Data Extension:

set @value = Lookup("Barcode Data","Barcode","SubscriberKey", _subscriberkey)
set @codabar = BarcodeURL(@value, "Codabar", 100, 50)
<img src="%%=v(@codabar)=%%">

Generate QR codes with AMPScript

AMPscript does not have a dedicated function that would allow for quick and easy QR code generation. Still, there are several other solutions that can be easily combined with AMPscript to get the results we need.

The first is using the publicly available, free API, QR Code Generator. All you need to do is send the data along with information about the size of the desired QR code to the qrserver API, and it will automatically generate a QR code on the fly, which can then be embedded inside an email or on a CloudPage:

To be able to use it inside an email or a CloudPage, all you need to do is pass the required data as a URL inside or an HTML <img> tag:

set @data = ';
set @qrcode = concat('×300&data=&#39;, @data)
<img src="%%=v(@qrcode)=%%">

Like any other AMPscript-based solution, you need to preview the email against a subscriber or publish the CloudPage for the QR code to display correctly.

If you are not able to use the above API for any reason, there are several JavaScript libraries that can be implemented in Marketing Cloud’s CloudPages, for example, the QRCode.js library.

In this setup, you should create a separate Code Resource to hold the actual QR code generating JS and a CloudPage where the actual QR code will be generated and displayed:

The qrcode.js file is a copy of the script from the QRCode.js library. I am not embedding the whole script here as it’s too long, but you can access it under this link:

On the CloudPage, you need to remember to refer to the link to your qrcode.js file. Then you can use AMPscript or SSJS to pull/define the data that should be passed to the QR code and lastly, call a javascript function to generate and display the QR code. Here’s an example of how this could be set up:

<script type="text/javascript" src="https://xxxxx/qrcode.js"></script>
set @data = ';
<div id="qrcode" style="width:300px; height:300px;"></div>
<span id="code"></span>
<script type="text/javascript">
var qrcode = new QRCode(document.getElementById("qrcode"), {
width : 300,
height : 300
function makeCode () {
var elText = "%%=v(@data)=%%";
var imgsrc = document.getElementById("qrcodesrc").src;
document.getElementById("code").innerHTML = imgsrc;
}, 500);

The above works very well on CloudPages. I have not tried using it inside an email, but it’s possible that it would also work there after making adequate changes.

Questions? Comments?

Leave a comment below or email me at

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s