Skip to content

Instantly share code, notes, and snippets.

@tresf
Last active May 17, 2023 04:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tresf/bd295ccd64d9e7b34ca74fea58366dab to your computer and use it in GitHub Desktop.
Save tresf/bd295ccd64d9e7b34ca74fea58366dab to your computer and use it in GitHub Desktop.
QZ Tray and DespatchCloud
const ws = require('ws');
const qz = require('qz-tray');

qz.api.setWebSocketType(ws);

fetch('https://production.courierapi.co.uk/api/couriers/v1/Test/create-label',
{
    method: 'POST',
    headers: {
        'api-user': 'demo',
        'api-token': '*********',
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(getRequest())
})
.then(response => response.text())
.then(data => {
    console.log(data.pngs);
    var obj = JSON.parse(data);
    var data = [];
    obj.pngs.forEach(base64 => {
        console.log("BASE64: " + base64);
        data.push(
            {
                type: 'pixel',
                format: 'image',
                flavor: 'base64',
                data: base64
            }
        );
    });
    var config = qz.configs.create("PDFwriter", { size: { width: 4, height: 6 } });
    return qz.websocket.connect().then(() => {
        return qz.print(config, data);
    });
})
.catch(e => {
    console.error(e);
});

function getRequest() {
    return {
        auth_company: "QZ Industries, LLC",
        request_id: "piocm5241li2dfj",
        format_address_default: true,
        shipment: {
            label_size: "6x4",
            label_format: "png",
            generate_invoice: false,
            generate_packing_slip: false,

            courier : {},

            collection_date: "2023-05-16T13:00:00.000Z",
            reference: "Paper rolls",
            delivery_instructions:  "Leave on side porch",
            ship_from: {
                name: "Lite Finocchiaro",
                phone: "3155106790",
                email: "lite@qz.io",
                company_name: "QZ Industries, LLC",
                address_1: "328 N. Peterboro St",
                address_2: "",
                address_3: "",
                city: "Canastota",
                postcode: "13032",
                county: "",
                country_iso: "US",
                company_id: "46-3087279",
                tax_id: "46-3087279"
            },
            ship_to: {
                name: "Tres Finocchiaro",
                phone: "3155106790",
                email: "tres@qz.io",
                company_name: "QZ Industries, LLC",
                address_1: "328 N. Peterboro St",
                address_2: "",
                address_3: "",
                city: "Canastota",
                county: "",
                postcode: "13032",
                country_iso: "US"
            },
            parcels: [
                {
                    dim_width: 6,
                    dim_height: 6,
                    dim_length: 6,
                    dim_unit: "in",

                    items: [
                        {
                            description: "Test Item One",
                            origin_country: "US",
                            quantity: 1,
                            value: 20,
                            value_currency: "USD",
                            weight: 0.6,
                            weight_unit: "lb",
                            sku: "TEST0001",
                            hs_code: "12345"
                        }
                    ]
                }
            ]
        }
    };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment