Search code examples
c#easypost

EasyPost API - print_custom_1 option won't print on label


This has been driving me batty for over an hour. I'm new to EasyPost and I'm trying to put some custom text on my label (in my particular case, it is which SKU to put in the package), but it just never seemed to work. I'm using the official nuget package from easypost but am guessing it is platform independent.

Shipment shipment = new Shipment() {
    to_address = toAddress,
    from_address = fromAddress,
    parcel = parcel
};

shipment.Create();
var lowestRate = shipment.LowestRate(includeServices: new List<string>() { "First" }, includeCarriers: new List<string>() { "USPS" });

shipment.Buy(lowestRate);

shipment.options.Add("print_custom_1", "this is some sample text");
shipment.options.Add("print_custom_2", "abc");
shipment.options.Add("print_custom_3", "xyz");

shipment.GenerateLabel("pdf");

Solution

  • Well, that was annoying. It makes sense when you step back from it. The issue is that the options need to be set PRIOR to creating the shipment. In my head, it was a print only concern (and it is), but there are other options can and do effect shipping costs, which means that the option needs to be set when creating the shipment. Even setting the options after you create but before you "buy" doesn't work.

    See working code below:

    Shipment shipment = new Shipment() {
        to_address = toAddress,
        from_address = fromAddress,
        parcel = parcel
    };
    
    //DO THIS BEFORE CREATING!
    shipment.options = new Dictionary<string, object>();
    shipment.options.Add("print_custom_1", "this is some sample text");
    shipment.options.Add("print_custom_2", "abc");
    shipment.options.Add("print_custom_3", "xyz");
    
    shipment.Create();
    var lowestRate = shipment.LowestRate(includeServices: new List<string>() { "First" }, includeCarriers: new List<string>() { "USPS" });
    
    shipment.Buy(lowestRate);
    
    shipment.GenerateLabel("pdf");