Search code examples
c#shippo

shippo api not returning all rates from active carriers


Hi im playing with the shippo api. I have 5 default carriers all active and in test mode. The thing is when i create a shipment and inspect all rates, im just getting the USPS rates and i would prefer rates from all carriers and compare the cost. Any ideas ? Regards

OK there is the code for those who are asking for it

using System;
using Shippo;
using System.Collections;
using Newtonsoft.Json.Linq;
using System.Linq;


namespace ShippoApiTest
{
class Program
{
    static void Main(string[] args)
    {
        string shippoPrivateAuthToken = "xxxxxxxxxxxxxxxxxxxxxxxxxx";

        try
        {
            APIResource resource = new APIResource(shippoPrivateAuthToken);

            Hashtable carriers = new Hashtable();
            carriers.Add("results", "20");
            var carriersAccounts = resource.AllCarrierAccount(carriers);

            foreach (var item in carriersAccounts.Data)
                Console.WriteLine(item.Carrier + " ----> " + item.AccountId + " -----> " + item.Active);

            // to address 
            Hashtable toAddressTable = new Hashtable();
            toAddressTable.Add("object_purpose", "PURCHASE");
            toAddressTable.Add("name", "Mr. Hippo");
            toAddressTable.Add("company", "Shippo");
            toAddressTable.Add("street1", "955 Benecia Avenue");
            toAddressTable.Add("city", "Sunnyvale");
            toAddressTable.Add("state", "CA");
            toAddressTable.Add("zip", "94085");
            toAddressTable.Add("country", "US");
            toAddressTable.Add("phone", "555 341 9393");
            toAddressTable.Add("email", "support@goshipppo.com");

            // from address 
            Hashtable fromAddressTable = new Hashtable();
            fromAddressTable.Add("object_purpose", "PURCHASE");
            fromAddressTable.Add("name", "Ms Hippo");
            fromAddressTable.Add("company", "San Diego Zoo");
            fromAddressTable.Add("street1", "503 Pine Street");
            fromAddressTable.Add("city", "San Diego");
            fromAddressTable.Add("state", "GA");
            fromAddressTable.Add("zip", "31312");
            fromAddressTable.Add("country", "US");
            fromAddressTable.Add("email", "hippo@goshipppo.com");
            fromAddressTable.Add("phone", "619 231 1515");
            fromAddressTable.Add("metadata", "Customer ID 123456");


            // parcel 
            Hashtable parcelTable = new Hashtable();
            parcelTable.Add("length", "5");
            parcelTable.Add("width", "5");
            parcelTable.Add("height", "5");
            parcelTable.Add("distance_unit", "in");
            parcelTable.Add("weight", "2");
            parcelTable.Add("mass_unit", "lb");

            // shipment 
            Hashtable shipmentTable = new Hashtable();
            shipmentTable.Add("address_to", toAddressTable);
            shipmentTable.Add("address_from", fromAddressTable);
            shipmentTable.Add("parcel", parcelTable);
            shipmentTable.Add("object_purpose", "PURCHASE");
            shipmentTable.Add("async", false);

            var allCarriersAccounts = carriersAccounts.Data.Select(c => c.ObjectId).ToArray();

            shipmentTable.Add("carrier_accounts", allCarriersAccounts);

            // create Shipment object 
            Console.WriteLine("Creating Shipment object..");
            Shipment shipment = resource.CreateShipment(shipmentTable);

            // select desired shipping rate according to your business logic 
            // we simply select the first rate in this example              
            //Rate rate = shipment.RatesList[0];
            //var rates = resource.AllRates(shipmentTable);

            foreach (var rate in shipment.RatesList)
            {
                Console.WriteLine((string)rate.Amount);
                Console.WriteLine((string)rate.AmountLocal);
                Console.WriteLine((string)rate.Currency);
                Console.WriteLine((string)rate.CurrencyLocal);
                Console.WriteLine((string)rate.Provider);
                Console.WriteLine((bool)rate.Trackable);
                Console.WriteLine((long)rate.Days);
                Console.WriteLine((string)rate.DurationTerms);
                Console.WriteLine((bool)rate.Insurance);

                Console.WriteLine((bool)rate.Insurance);
                Console.WriteLine((string)rate.ServicelevelName);
                Console.WriteLine((string)rate.ServicelevelTerms);

                Console.WriteLine("Attributes: ");
                foreach (var attr in new JArray(rate.Attributes))
                    foreach (var token in attr.Children())
                        Console.Write(token);

                Console.WriteLine("\n\n\n");
            }

            var currentRate = shipment.RatesList[0];

            Console.WriteLine("Getting shipping label..");
            Hashtable transactionParameters = new Hashtable();
            transactionParameters.Add("rate", currentRate.ObjectId);
            transactionParameters.Add("async", false);
            Transaction transaction = resource.CreateTransaction(transactionParameters);

            if (((String)transaction.ObjectStatus).Equals("SUCCESS", StringComparison.OrdinalIgnoreCase))
            {
                Console.WriteLine("Label url : " + transaction.LabelURL);
                Console.WriteLine("Tracking number : " + transaction.TrackingNumber);
            }
            else
            {
                Console.WriteLine("An Error has occured while generating your label. Messages : " + transaction.Messages);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            Console.ReadLine();
        }

        Console.ReadLine();
    }
}
}

All my carriers accounts


Solution

  • There could be different reasons why you don't see other rates. The first things that come to mind are:

    1. You're explicitly listing all carrier accounts in the Shipment API call (shipmentTable.Add("carrier_accounts", allCarriersAccounts);). Given that you want to see rates for all carriers that's unnecessary, as Shippo will automatically use all of our accounts if you don't send the carrier_accounts attribute. Thus I suggest removing this line to eliminate any potential bugs related to this line.
    2. I don't know which other carriers you have activated on Shippo but potential these other carriers don't support US domestic shipments. E.g. DHL Express only supports international shipments, Canada Post only shipments from Canada, etc.

    Hope that helps! Happy to provide further assistance if you have more questions.