REST API printing

REST API printing endpoints are a powerful tool that enables application integration with any data source capable of sending a simple request to a specified address in the network. Thanks to this, JustLabel can work with any client’s system, regardless of the language of its implementation and the architecture used.

Printing process using REST API

The application launched in the server or desktop version provides public API enabling integration with any client system. In short, the printing process looks like this:

  1. The customer creates label templates in the JustLabel application and configures printers by naming them. A print queue is automatically created for each added printer.
  2. Any client system (ERP System, WMS System, custom application, etc.) is configured to send requests containing the name of the print queue, the name of the label template and the JSON body with data for placeholders to the appropriate network address.
  3. The application downloads the appropriate data, reads the label template, substitutes the JSON data into the appropriate places, converts the whole into the final ZPL code and sends it to the appropriate printer over the network.

Please note that this solution is completely independent of client and server operating systems, programming languages and physical location of devices. The only requirement for printing is the ability to connect all these devices with the ethernet network.

Explore the endpoints

You can access the REST API documentation on the documentation page and directly on the local machine or server machine running the application. To view the documentation page provided by the application, you need to know the host address and the port number of the running JustLabel instance. For the desktop version, this will be localhost:5987. In any web browser, visit the mentioned address with the additional path /swagger-ui/index.html. The final documentation address for the desktop version should look like this:

http://localhost:5987/swagger-ui/index.html

In the examples of this tutorial, we assume the desktop version and use the presented address. If the application is running in the server version, in the above address, localhost:5987 should be replaced with the server’s host name or IP address and appropriate port number.

After visiting this page, a content similar to the one presented below should be displayed. There are descriptions of REST API endpoints and data transfer objects (DTO).

Remember, you can test the endpoints directly from this page. Just use the Try it out button on desired endpoint, fill in all the necessary data and press Execute to send the request.

Tutorial example dataset

This tutorial is based on highly personalized settings. The user can use the desktop or server version of the program, he can have any names of printers and print queues, he can have any names of labels as well as any names of placeholders. Therefore, we need to prepare a set of data on which we will build the following examples. Below we present a set of exemplary data along with a description of individual elements. This will allow you to easily transfer the logic of the examples to real customer use cases.

  • host – the name (or IP) of the machine, where the JustLabel instance runs – our example: localhost
  • port – the port of JustLabel instance – our example: 5987
  • printer – the name of printer defined in Printers tab – our example: testPrinter
  • printer queue – the name of printer queue, which by default is automatically created in Printouts tab – our example: pq_testPrinter
  • template – the name of label template, we want to print – our example: testTemplate
  • placeholders – the names of placeholders used in the template – our example placeholders and values pairs:
    • namePlaceholder = “The Name
    • descriptionPlaceholder = “The Description

Simple print endpoint

The simple print endpoint allows user to print one copy of a label with specified template and printer queue. The endpoint path is

/simple-print/{printerQueueName}

To print using this endpoint, there are some parts of a request that have to be provided:

  1. printerQueueName path parameter – print queue name to tell the program which printer you want to print to. Our example: pq_testPrinter
  2. templateName query parameter – the name of the template, which should be printed. Our example: testTemplate
  3. request body – a JSON request body object containing the template’s placeholders list with the name and value pairs, wrapped in brackets: [ ]. Our example placeholders are namePlaceholder and descriptionPlaceholder

Finally, the simple-print request for our example data should look like this:

URL

http://localhost:5987/simple-print/pq_testPrinter?templateName=testTemplate

JSON

[
  {
    "namePlaceholder": "The Name",
    "descriptionPlaceholder": "The Description",
  },
]

Standard print endpoint

The print endpoint allows user to print multiple copies of a label using template or raw data. In this section we will discuss the template version. The endpoint path is

/print/{printerQueueName}

To print using this endpoint, there are some parts of a request that have to be provided:

  1. printerQueueName path parameter – print queue name to tell the program which printer you want to print to. Our example: pq_testPrinter
  2. request body – a JSON request body containing the necessary template-print information. Request body schema for the template print version looks like this:
{
  "template": {
    "name": "string",
    "options": [
      {
        "numberOfCopies": 0,
        "placeholderValues": {
          "additionalProp1": "string",
          "additionalProp2": "string",
          "additionalProp3": "string"
        }
      }
    ]
  }
}

As you can see, the JSON body is a bit more complicated in this version than in simple-print, but you will easily understand everything by looking at the example supplemented with our sample data.

  • query parameter templateName has been included here in the JSON body as a field of the template object and has the short name name. In our case, it will take the value of testTemplate
  • there is a new parameter called numberOfCopies which simply indicates how many copies of the label you want to print
  • in placeholderValues object you can specify the names and values of template’s placeholders. In our case, the namePlaceholder and descriptionPlaceholder will be included

The final version of print endpoint request looks like this:

URL

http://localhost:5987/print/pq_testPrinter

JSON

{
  "template": {
    "name": "testTemplate",
    "options": [
      {
        "numberOfCopies": 1,
        "placeholderValues": {
          "namePlaceholder": "The Name",
          "descriptionPlaceholder": "The Description"
        }
      }
    ]
  }
}

Print with raw data

The print endpoint described in previous section allows also the raw data print. It means, the application would not modify the given data but it will send it directly to the printer. Following logic from the previous example, you have to point the printer using the path parameter, so the URL doesn’t change and our example printer queue is pq_testPrinter:

URL

http://localhost:5987/print/pq_testPrinter

In the JSON body however, you don’t use the template object. You should use raw object instead and put the raw ZPL code as the value of the code field. We will use the ZPL code for an empty label just to demonstrate the example JSON body with raw code object.

JSON

{
  "raw": [
    {
      "numberOfCopies": 1,
      "code":  "^XA
                ^CI28
                ^PON
                ^BY2,3,0,10
                ^LH0,0
                ^FO10,10^A0N,30,30^FH^FD_53_6F_6D_65_20^FS
                ^XZ"
    }
  ]
}

Secure printers with API Key

Sometimes it is necessary to prevent unwanted print requests. Imagine a situation that you have developed an application that makes printouts using the REST API, but a new version has already been created and you want to deny print permission to all previously released programs or you just want to prevent any unauthorized printouts of any other reason.

You can achieve this simply by securing the printer queue using the API Key. The description on how to do this can be found in this tutorial.

If your print queue is already secured by API Key, printouts without an authorization key will be rejected. If print authorization is required, the value of the API Key should be appended to each print request as one in the header named X-API-Key

Table of contents