Generate an EAN-13 Barcode in Swift

1 min readJun 18, 2021

EAN barcodes were created for use in Europe but are recognized internationally. Similar to the UPC formats, EAN codes only encode numeric data, which enables an effortless scanning process. The EAN-13 barcode contains 13 digits that represent the country code, product and manufacturer information, and checksum. In the following tutorial, we will demonstrate how you can use an API in Swift to instantly generate an EAN-13 barcode image for your tracking convenience.

To run the API and call the function, input your barcode value into the below example code:

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
var semaphore = DispatchSemaphore (value: 0)let parameters = "0=%3C&1=s&2=t&3=r&4=i&5=n&6=g&7=%3E"
let postData = .utf8)
var request = URLRequest(url: URL(string: "")!,timeoutInterval: Double.infinity)
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.addValue("YOUR-API-KEY-HERE", forHTTPHeaderField: "Apikey")
request.httpMethod = "POST"
request.httpBody = postData
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else {
print(String(describing: error))
print(String(data: data, encoding: .utf8)!)

And just like that, a PNG file of your EAN-13 barcode will be ready for download. Easy!




There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.