Create Barcodes in Python

Python sample code to create barcodes with the McAPI Barcode REST API service.

Requirements: A RapidAPI account. Replace YOUR_API_KEY in the snippets below with your RapidAPI key.

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

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

Create a Barcode using Python with the requests module

Shown is the creation of a Code 128 (code type: 7) with Python 3 encoding the text "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 Python 3
  
import requests

url = 'https://mcapi-barcode.p.rapidapi.com/'

payload = '{\
  "data": "123456",\
  "type": 7,\
  "moduleWidth": 0.5,\
  "moduleHeight": 20,\
  "format": "pdf"\
}'
headers = {
  'content-type': 'application/json',
  'x-rapidapi-key': 'YOUR_API_KEY',
  'x-rapidapi-host': 'mcapi-barcode.p.rapidapi.com'
}

response = requests.request('POST', url, data=payload.encode('utf-8'), headers=headers)

The data will be delivered in response.text, e.g.:

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

The returned code from the Python request:

Sample Barcode Python

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