Skip to content

Latest commit

 

History

History
222 lines (170 loc) · 8.75 KB

royal-mail-tracking-api-java.md

File metadata and controls

222 lines (170 loc) · 8.75 KB

Royal Mail Tracking API - Java

Use Java to track Royal Mail shipments with Royal Mail Tracking API.

Features

  • Real-time Royal Mail tracking.
  • Batch Royal Mail tracking.
  • Other features to manage your Royal Mail tracking.

Installation

Maven

$ <dependency>
$ <groupId>io.github.trackingmores</groupId>
$ <artifactId>trackingmore-sdk-java</artifactId>
$ <version>1.0.3</version>
$ </dependency>

Gradle

$ implementation "io.github.trackingmores:trackingmore-sdk-java:1.0.3"

Quick Start

Get the API key:

To use this API, you need to generate your API key.

  • Click here to access TrackingMore admin.
  • Go to the "Developer" section.

  • Click "Generate API Key".

  • Give a name to your API key, and click "Save" .

Then, start to track your Royal Mail shipments.

Usage

Create a tracking (Real-time tracking):

package com.trackingmore.example.royalmail;

import com.trackingmore.TrackingMore;
import com.trackingmore.exception.TrackingMoreException;
import com.trackingmore.model.TrackingMoreResponse;

import java.io.IOException;
import java.util.List;

public class CreateRoyalMailTrackingExample {

    public static void main(String[] args) {
        try {
            String apiKey = "your api key";
            TrackingMore trackingMore = new TrackingMore(apiKey);
            
            CreateTrackingParams createTrackingParams = new CreateTrackingParams();
            createTrackingParams.setTrackingNumber("RF423511785GB");
            createTrackingParams.setCourierCode("royal-mail");
            TrackingMoreResponse<Tracking> result = trackingMore.trackings.CreateTracking(createTrackingParams);
            System.out.println(result.getMeta().getCode());
            if(result.getData() != null){
               Tracking trackings = result.getData();
               System.out.println(trackings);
               System.out.println(trackings.getTrackingNumber());
            }
        } catch (TrackingMoreException e) {
            System.err.println("error:" + e.getMessage());
        } catch (IOException e) {
            System.err.println("error:" + e.getMessage());
        }
    }

}

Create trackings (Max. 40 tracking numbers create in one call):

package com.trackingmore.example.royalmail;

import com.trackingmore.TrackingMore;
import com.trackingmore.exception.TrackingMoreException;
import com.trackingmore.model.TrackingMoreResponse;

import java.io.IOException;
import java.util.List;

public class CreateRoyalMailTrackingsExample {

    public static void main(String[] args) {
        try {
            String apiKey = "your api key";
            TrackingMore trackingMore = new TrackingMore(apiKey);
            
            List<CreateTrackingParams> paramsList = new ArrayList<>();
            
            CreateTrackingParams createTrackingParams1 = new CreateTrackingParams();
            createTrackingParams1.setTrackingNumber("KL698957554GB");
            createTrackingParams1.setCourierCode("royal-mail");
            
            CreateTrackingParams createTrackingParams2 = new CreateTrackingParams();
            createTrackingParams2.setTrackingNumber("FL356951328GB");
            createTrackingParams2.setCourierCode("royal-mail");
            
            paramsList.add(createTrackingParams1);
            paramsList.add(createTrackingParams2);
            
            TrackingMoreResponse<BatchResults> result = trackingMore.trackings.BatchCreateTrackings(paramsList);
            System.out.println(result.getMeta().getCode());
            BatchResults batchResults = result.getData();
            if(result.getData() != null){
                System.out.println(batchResults);
                for (BatchItem batchItem : batchResults.getSuccess()) {
                    String trackingNumber = batchItem.getTrackingNumber();
                    String courierCode = batchItem.getCourierCode();
                    System.out.println("Tracking Number: " + trackingNumber);
                    System.out.println("Courier Code: " + courierCode);
                }
                for (BatchItem batchItem : batchResults.getError()) {
                    String trackingNumber = batchItem.getTrackingNumber();
                    String courierCode = batchItem.getCourierCode();
                    System.out.println("Tracking Number: " + trackingNumber);
                    System.out.println("Courier Code: " + courierCode);
                }
            }
        } catch (TrackingMoreException e) {
            System.err.println("error:" + e.getMessage());
        } catch (IOException e) {
            System.err.println("error:" + e.getMessage());
        }
    }
}

Get status of the shipment:

package com.trackingmore.example.royalmail;

import com.trackingmore.TrackingMore;
import com.trackingmore.exception.TrackingMoreException;
import com.trackingmore.model.TrackingMoreResponse;

import java.io.IOException;
import java.util.List;

public class GetRoyalMailTrackingExample {

    public static void main(String[] args) {
        try {
            String apiKey = "your api key";
            TrackingMore trackingMore = new TrackingMore(apiKey);
            
            GetTrackingResultsParams trackingParams = new GetTrackingResultsParams();
            # Perform queries based on various conditions
            # trackingParams.setTrackingNumbers("FL356951328GB,KL698957554GB");
            trackingParams.setCourierCode("royal-mail");
            trackingParams.setCreatedDateMin("2023-08-23T06:00:00+00:00");
            trackingParams.setCreatedDateMax("2023-09-05T07:20:42+00:00");
            TrackingMoreResponse<List<Tracking>> result = trackingMore.trackings.GetTrackingResults(trackingParams);
            System.out.println(result.getMeta().getCode());
            List<Tracking> trackings = result.getData();
            for (Tracking tracking : trackings) {
                String trackingNumber = tracking.getTrackingNumber();
                String courierCode = tracking.getCourierCode();
                
                System.out.println("Tracking Number: " + trackingNumber);
                System.out.println("Courier Code: " + courierCode);
            }
        } catch (TrackingMoreException e) {
            System.err.println("error:" + e.getMessage());
        } catch (IOException e) {
            System.err.println("error:" + e.getMessage());
        }
    }
}

Update a tracking by ID:

package com.trackingmore.example.royalmail;

import com.trackingmore.TrackingMore;
import com.trackingmore.exception.TrackingMoreException;
import com.trackingmore.model.TrackingMoreResponse;

import java.io.IOException;
import java.util.List;

public class UpdateRoyalMailTrackingExample {

    public static void main(String[] args) {
        try {
            String apiKey = "your api key";
            TrackingMore trackingMore = new TrackingMore(apiKey);
            
            String idString = "9a134772fec0a75ba36ed8162151eb87";
            UpdateTrackingParams updateTrackingParams = new UpdateTrackingParams();
            updateTrackingParams.setCustomerName("New name");
            updateTrackingParams.setNote("New tests order note");
            TrackingMoreResponse<UpdateTracking> result = trackingMore.trackings.UpdateTrackingByID(idString, updateTrackingParams);
            System.out.println(result.getMeta().getCode());
            System.out.println(result.getData());
            if(result.getData() != null){
                UpdateTracking  updateTracking= result.getData();
                System.out.println(updateTracking);
                System.out.println(updateTracking.getTrackingNumber());
            }
        } catch (TrackingMoreException e) {
            System.err.println("error:" + e.getMessage());
        } catch (IOException e) {
            System.err.println("error:" + e.getMessage());
        }
    }
}