Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Node.js Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Node.js) UPS Tracking API

Demonstrates making a call to the UPS tracking REST API. Parses the tracking response and extracts the base64 signature image to a gif file.

Install Chilkat for Node.js and Electron using npm at

Chilkat npm packages for Node.js

Chilkat npm packages for Electron

on Windows, Linux, MacOSX, and ARM

var os = require('os');
if (os.platform() == 'win32') {  
    if (os.arch() == 'ia32') {
        var chilkat = require('@chilkat/ck-node21-win-ia32');
    } else {
        var chilkat = require('@chilkat/ck-node21-win64'); 
    }
} else if (os.platform() == 'linux') {
    if (os.arch() == 'arm') {
        var chilkat = require('@chilkat/ck-node21-arm');
    } else if (os.arch() == 'x86') {
        var chilkat = require('@chilkat/ck-node21-linux32');
    } else {
        var chilkat = require('@chilkat/ck-node21-linux64');
    }
} else if (os.platform() == 'darwin') {
    if (os.arch() == 'arm64') {
        var chilkat = require('@chilkat/ck-node21-mac-m1');
    } else {
        var chilkat = require('@chilkat/ck-node21-macosx');
    }
}

function chilkatExample() {

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    var success;

    var http = new chilkat.Http();

    // This is the testing endpoint for the tracking API:
    var url = "https://wwwcie.ups.com/rest/Track";

    // Send an HTTP request with the following JSON body:

    // {
    //   "UPSSecurity": {
    //     "UsernameToken": {
    //       "Username": "Your Username",
    //       "Password": "Your Password"
    //     },
    //     "ServiceAccessToken": {
    //       "AccessLicenseNumber": "Your Access License Number"
    //     }
    //   },
    //   "TrackRequest": {
    //     "Request": {
    //       "RequestOption": "1",
    //       "TransactionReference": {
    //         "CustomerContext": "Your Test Case Summary Description"
    //       }
    //     },
    //     "InquiryNumber": "YourTrackingNumber"
    //   }
    // }
    // 

    // Build the above JSON.
    var json = new chilkat.JsonObject();
    json.UpdateString("UPSSecurity.UsernameToken.Username","UPS_USERNAME");
    json.UpdateString("UPSSecurity.UsernameToken.Password","UPS_PASSWORD");
    json.UpdateString("UPSSecurity.ServiceAccessToken.AccessLicenseNumber","UPS_ACCESS_KEY");
    // Request all activity...
    json.UpdateString("TrackRequest.Request.RequestOption","activity");
    json.UpdateString("TrackRequest.Request.TransactionReference.CustomerContext","Your Test Case Summary Description");
    json.UpdateString("TrackRequest.InquiryNumber","1Z12345E0205271688");

    var sb = new chilkat.StringBuilder();
    // resp: HttpResponse
    var resp = http.PostJson3(url,"application/json",json);
    if (http.LastMethodSuccess !== true) {
        console.log(http.LastErrorText);
        return;
    }

    console.log("status = " + resp.StatusCode);

    // A 200 response status indicate success.
    if (resp.StatusCode !== 200) {
        console.log(resp.BodyStr);
        console.log("Failed.");

        return;
    }

    json.Load(resp.BodyStr);
    json.EmitCompact = false;
    console.log(json.Emit());

    // {
    //   "TrackResponse": {
    //     "Response": {
    //       "ResponseStatus": {
    //         "Code": "1",
    //         "Description": "Success"
    //       },
    //       "TransactionReference": {
    //         "CustomerContext": "Your Test Case Summary Description"
    //       }
    //     },
    //     "Shipment": {
    //       "InquiryNumber": {
    //         "Code": "01",
    //         "Description": "ShipmentIdentificationNumber",
    //         "Value": "1Z12345E0205271688"
    //       },
    //       "ShipmentType": {
    //         "Code": "01",
    //         "Description": "Small Package"
    //       },
    //       "ShipperNumber": "12345E",
    //       "Service": {
    //         "Code": "002",
    //         "Description": "2ND DAY AIR"
    //       },
    //       "ReferenceNumber": {
    //         "Code": "01",
    //         "Value": "LINE4AND115"
    //       },
    //       "PickupDate": "19990608",
    //       "Package": {
    //         "TrackingNumber": "1Z12345E0205271688",
    //         "Activity": [
    //           {
    //             "ActivityLocation": {
    //               "Address": {
    //                 "City": "ANYTOWN",
    //                 "StateProvinceCode": "GA",
    // 
    //                 "PostalCode": "30340",
    //                 "CountryCode": "US"
    //               },
    //               "Code": "ML",
    //               "Description": "BACK DOOR",
    //               "SignedForByName": "JOHN DOE"
    //             },
    //             "Status": {
    //               "Type": "D",
    //               "Description": "DELIVERED",
    //               "Code": "KM"
    //             },
    //             "Date": "19990610",
    //             "Time": "120000",
    //             "Document": {
    //               "Type": {
    //                 "Code": "01",
    //                 "Description": "Signature Image"
    //               },
    //               "Content": "R0lGODdhoA ... JU9Y8RdHsRKLMVJ4MVDMREAAADs=",
    //               "Format": {
    //                 "Code": "01",
    //                 "Description": "GIF"
    //               }
    //             }
    //           },
    //           {
    //             "Status": {
    //               "Type": "M",
    //               "Description": "BILLING INFORMATION RECEIVED. SHIPMENT DATE PENDING.",
    //               "Code": "MP"
    //             },
    //             "Date": "19990608",
    //             "Time": "120000"
    //           }
    //         ],
    //         "PackageWeight": {
    //           "UnitOfMeasurement": {
    //             "Code": "LBS"
    //           },
    //           "Weight": "5.00"
    //         },
    //         "ReferenceNumber": [
    //           {
    //             "Code": "01",
    //             "Value": "LINE4AND115"
    //           },
    //           {
    //             "Code": "08",
    //             "Value": "LJ67Y5"
    //           }
    //         ]
    //       }
    //     },
    //     "Disclaimer": "You are using UPS tracking service on customer integration test environment, please switch to UPS production environment once you finish the test. The URL is https://onlinetools.ups.com/webservices/Track"
    //   }
    // }

    // Use the online tool at Generate JSON Parsing Code
    // to generate JSON parsing code.

    var statusCode = json.StringOf("TrackResponse.Response.ResponseStatus.Code");
    var statusDescription = json.StringOf("TrackResponse.Response.ResponseStatus.Description");
    console.log("statusCode: " + statusCode);
    console.log("statusDescription" + statusDescription);
    var customerContext = json.StringOf("TrackResponse.Response.TransactionReference.CustomerContext");
    var inquiryNumberCode = json.StringOf("TrackResponse.Shipment.InquiryNumber.Code");
    var inquiryNumberDescription = json.StringOf("TrackResponse.Shipment.InquiryNumber.Description");
    var inquiryNumberValue = json.StringOf("TrackResponse.Shipment.InquiryNumber.Value");
    var shipmentTypeCode = json.StringOf("TrackResponse.Shipment.ShipmentType.Code");
    var shipmentTypeDescription = json.StringOf("TrackResponse.Shipment.ShipmentType.Description");
    var shipperNumber = json.StringOf("TrackResponse.Shipment.ShipperNumber");
    var serviceCode = json.StringOf("TrackResponse.Shipment.Service.Code");
    var serviceDescription = json.StringOf("TrackResponse.Shipment.Service.Description");
    var referenceNumberCode = json.StringOf("TrackResponse.Shipment.ReferenceNumber.Code");
    var referenceNumberValue = json.StringOf("TrackResponse.Shipment.ReferenceNumber.Value");
    var pickupDate = json.StringOf("TrackResponse.Shipment.PickupDate");
    var trackingNumber = json.StringOf("TrackResponse.Shipment.Package.TrackingNumber");
    var unitOfMeasurementCode = json.StringOf("TrackResponse.Shipment.Package.PackageWeight.UnitOfMeasurement.Code");
    var weight = json.StringOf("TrackResponse.Shipment.Package.PackageWeight.Weight");
    var disclaimer = json.StringOf("TrackResponse.Disclaimer");

    var i = 0;
    var activityCount = json.SizeOfArray("TrackResponse.Shipment.Package.Activity");
    console.log("activityCount: " + activityCount);

    while (i < activityCount) {
        console.log("-- activity " + i);
        json.I = i;
        if (json.HasMember("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City") == true) {
            var city = json.StringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City");
            console.log("city: " + city);
            var provinceCode = json.StringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.StateProvinceCode");
            var postalCode = json.StringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.PostalCode");
            var countryCode = json.StringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.CountryCode");
        }

        var locationCode = json.StringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Code");
        var locationDescription = json.StringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Description");
        var locationSignedForByName = json.StringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.SignedForByName");

        var activityStatusType = json.StringOf("TrackResponse.Shipment.Package.Activity[i].Status.Type");
        console.log("activityStatusType: " + activityStatusType);
        var activityStatusDescription = json.StringOf("TrackResponse.Shipment.Package.Activity[i].Status.Description");
        console.log("activityStatusDescription: " + activityStatusDescription);
        var activityStatusCode = json.StringOf("TrackResponse.Shipment.Package.Activity[i].Status.Code");
        console.log("activityStatusCode: " + activityStatusCode);

        var activityDate = json.StringOf("TrackResponse.Shipment.Package.Activity[i].Date");
        var activityTime = json.StringOf("TrackResponse.Shipment.Package.Activity[i].Time");

        if (json.HasMember("TrackResponse.Shipment.Package.Activity[i].Document") == true) {
            var typeCode = json.IntOf("TrackResponse.Shipment.Package.Activity[i].Document.Type.Code");
            var typeDescription = json.StringOf("TrackResponse.Shipment.Package.Activity[i].Document.Type.Description");
            var documentContent = json.StringOf("TrackResponse.Shipment.Package.Activity[i].Document.Content");
            var documentFormatCode = json.StringOf("TrackResponse.Shipment.Package.Activity[i].Document.Format.Code");
            // Format description would be something like "GIF" for a signature image.
            var documentFormatDescription = json.StringOf("TrackResponse.Shipment.Package.Activity[i].Document.Format.Description");

            // 01 - Signature Image
            // 02 - Delivery Receipt
            // 03 - Free Astray
            // 04 - POD
            if (typeCode == 1) {
                // We have a signature image.  Get the image data and save to a file.
                var sbImagePath = new chilkat.StringBuilder();
                sbImagePath.Append("qa_output/sig_");
                sbImagePath.Append(trackingNumber);
                sbImagePath.Append(".");
                sbImagePath.Append(documentFormatDescription);
                var imageData = new chilkat.BinData();
                success = imageData.AppendEncoded(documentContent,"base64");
                // Write to "qa_output/sig_1Z12345E0205271688.GIF"
                success = imageData.WriteFile(sbImagePath.GetAsString());
            }

        }

        i = i+1;
    }

    i = 0;
    var refnumCount = json.SizeOfArray("TrackResponse.Shipment.Package.ReferenceNumber");
    while (i < refnumCount) {
        json.I = i;
        var refnumCode = json.StringOf("TrackResponse.Shipment.Package.ReferenceNumber[i].Code");
        var refnumValue = json.StringOf("TrackResponse.Shipment.Package.ReferenceNumber[i].Value");
        i = i+1;
    }

    console.log("Success.");


}

chilkatExample();

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.