Automated Shipping Calculations

    Use a shipping provider to automatically calculate shipping rates on your Stripe orders.

    Calculating shipping rates on your orders often means keeping several systems in sync or charging a flat-rate amount that either over- or under-charges your customer. If you already have your products represented in Stripe, we can help you calculate accurate shipping rates from a number of carriers without doing any additional work. Stripe has partnered with EasyPost and Shippo to help you get shipping rates directly from USPS, UPS, FedEx, and other carriers for your Stripe orders.

    You can represent products and orders within Stripe, rather than on your own site or through a third-party. If you are not yet familiar with the APIs for products and orders, we recommend that you first visit the Orders API documentation before diving into shipping cost calculations.

    To enable dynamic shipping rates for your orders, follow our guides to get started with EasyPost or Shippo. You can optionally read on below to learn how the integration works.

    How the integration works

    Stripe’s shipping integrations automatically manage everything necessary to calculate shipping costs for your orders. To help explain how the integration functions, read the high-level and technical overviews below. (The latter is intended for developers.)

    High-level overview

    When you create an order via Stripe, Stripe programmatically sends the following details to your shipping provider:

    • The specific versions (as SKUs) of the products being ordered
    • Your From address as specified in the Dashboard
    • The customer’s destination shipping address

    Stripe uses the addresses provided along with the weight and dimensions of the SKUs to calculate shipping costs through your shipping provider. The shipping provider returns the shipping methods to Stripe, which are added to the order in real-time. You can let your customers choose between the returned shipping methods, and Stripe will automatically update your order total as you select different shipping methods.

    The shipping rates calculated by the shipping provider depend on the size and weight of your products. It is therefore important that you provide Stripe with accurate dimensions for your products. You can do this from the Dashboard or the Stripe API. To use the Dashboard, navigate to a product page and click the Edit product button. Make sure that the Shippable checkbox is checked and fill in the width, height, length, and weight at the bottom of the page.

    To use the API, perform an update product request like the one below. In the API, we assume that width, height, and length are in inches while weight is in ounces:

    curl https://api.stripe.com/v1/products/pr_1BRElB2eZvKYlo2Coe8wr2mO \
      -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \
      -d package_dimensions[height]=19 \
      -d package_dimensions[length]=15 \
      -d package_dimensions[width]=15 \
      -d package_dimensions[weight]=32
    
    # Set your secret key: remember to change this to your live secret key in production
    # See your keys here: https://dashboard.stripe.com/account/apikeys
    Stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
    
    Stripe::Product.update('pr_1BRElB2eZvKYlo2Coe8wr2mO', {
        package_dimensions: {
            height: 19,
            length: 15,
            width: 15,
            weight: 32,
        },
    })
    
    # Set your secret key: remember to change this to your live secret key in production
    # See your keys here: https://dashboard.stripe.com/account/apikeys
    stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
    
    stripe.Product.modify('pr_1BRElB2eZvKYlo2Coe8wr2mO',
      package_dimensions={
        "height": 19,
        "length": 15,
        "width": 15,
        "weight": 32,
      },
    )
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    \Stripe\Stripe::setApiKey('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
    
    \Stripe\Product::update('pr_1BRElB2eZvKYlo2Coe8wr2mO', [
        'package_dimensions' => [
            'height' => 19,
            'length' => 15,
            'width' => 15,
            'weight' => 32,
        ],
    ]);
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    Stripe.apiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc";
    
    Map<String, Object> packageParams = new HashMap<>();
    packageParams.put("height", 19);
    packageParams.put("length", 15);
    packageParams.put("width", 15);
    packageParams.put("weight", 32);
    Map<String, Object> params = new HashMap<>();
    params.put("package_dimensions", packageParams);
    Product product = Product.update("pr_1BRElB2eZvKYlo2Coe8wr2mO", params);
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    const stripe = require('stripe')('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
    
    stripe.products.update('pr_1BRElB2eZvKYlo2Coe8wr2mO', {
      package_dimensions: {
        height: 19,
        length: 15,
        width: 15,
        weight: 32,
      },
    });
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    stripe.Key = "sk_test_4eC39HqLyjWDarjtT1zdp7dc"
    
    params := &stripe.ProductParams{
        PackageDimensions: &stripe.PackageDimensionsParams{
            Height: stripe.Float64(19),
            Length: stripe.Float64(15),
            Width: stripe.Float64(15),
            Weight: stripe.Float64(32),
        },
    }
    prod, _ := product.Update("pr_1BRElB2eZvKYlo2Coe8wr2mO", params)
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    StripeConfiguration.SetApiKey("sk_test_4eC39HqLyjWDarjtT1zdp7dc");
    
    var options = new ProductUpdateOptions {
        PackageDimensions = new PackageDimensionOptions {
            Height = 19,
            Length = 15,
            Width = 15,
            Weight = 32,
        },
    };
    var service = new ProductService();
    Product product = service.Update("pr_1BRElB2eZvKYlo2Coe8wr2mO", options);
    

    This shipping cost calculation service is usually provided free of charge by the shipping provider, so using a shipping integration with Stripe in most cases does not cost you anything. In addition to getting shipping rates, some providers let you purchase and print shipping labels online. This process varies between providers, and we encourage you to read the specific guides for EasyPost and Shippo to learn how label generation is supported.

    Technical overview

    Once you set up your shipping integration, you should automatically see multiple shipping methods show up in your order’s shipping_methods array. For example, the following request creates an order shippable to a U.S. address:

    curl https://api.stripe.com/v1/orders \
      -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \
      -d currency=usd \
      -d email="jenny.rosen@example.com" \
      -d items[][type]=sku \
      -d items[][parent]=sku_FLlHo2wdASn5mO \
      -d items[][quantity]=2 \
      -d shipping[name]="Jenny Rosen" \
      -d shipping[address][line1]="1234 Main Street" \
      -d shipping[address][city]="San Francisco" \
      -d shipping[address][state]=CA \
      -d shipping[address][postal_code]=94111 \
      -d shipping[address][country]=US
    
    # Set your secret key: remember to change this to your live secret key in production
    # See your keys here: https://dashboard.stripe.com/account/apikeys
    Stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
    
    order = Stripe::Order.create({
        currency: 'usd',
        email: 'jenny.rosen@example.com',
        items: [
            {
                type: 'sku',
                parent: 'sku_FLlHo2wdASn5mO',
                quantity: 2,
            },
        ],
        shipping: {
            name: 'Jenny Rosen',
            address: {
                line1: '1234 Main Street',
                city: 'San Francisco',
                state: 'CA',
                postal_code: '94111',
                country: 'US',
            },
        },
    })
    
    # Set your secret key: remember to change this to your live secret key in production
    # See your keys here: https://dashboard.stripe.com/account/apikeys
    stripe.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'
    
    order = stripe.Order.create(
      currency='usd',
      email='jenny.rosen@example.com',
      items=[
        {
          'type':'sku',
          'parent':'sku_FLlHo2wdASn5mO',
          'quantity': 2,
        },
      ],
      shipping={
        'name':'Jenny Rosen',
        'address':{
          'line1':'1234 Main Street',
          'city':'San Francisco',
          'state':'CA',
          'postal_code':'94111',
          'country':'US',
        },
      },
    )
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    \Stripe\Stripe::setApiKey('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
    
    $order = \Stripe\Order::create([
        'currency' => 'usd',
        'email' => 'jenny.rosen@example.com',
        'items' => [
            [
                'type' => 'sku',
                'parent' => 'sku_FLlHo2wdASn5mO',
                'quantity' => 2,
            ],
        ],
        'shipping' =>  [
            'name' => 'Jenny Rosen',
            'address' => [
                'line1' => '1234 Main Street',
                'city' => 'San Francisco',
                'state' => 'CA',
                'postal_code' => '94111',
                'country' => 'US',
            ],
        ],
    ]);
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    Stripe.apiKey = "sk_test_4eC39HqLyjWDarjtT1zdp7dc";
    
    Map <String, Object> params = new HashMap<>();
    params.put("currency", "usd");
    params.put("email", "jenny.rosen@example.com");
    Map<String, String> item = new HashMap<>();
    item.put("type", "sku");
    item.put("parent", "sku_FLlHo2wdASn5mO");
    item.put("quantity", "2");
    List<Object> itemsParams = new LinkedList<Object>();
    itemsParams.add(item);
    params.put("items", itemsParams);
    Map<String, Object> shippingParams = new HashMap<>();
    shippingParams.put("name", "Jenny Rosen");
    Map<String, Object> addressParams = new HashMap<>();
    addressParams.put("line1", "1234 Main Street");
    addressParams.put("city", "San Francisco");
    addressParams.put("state", "CA");
    addressParams.put("postal_code", "94111");
    addressParams.put("country", "US");
    shippingParams.put("address", addressParams);
    params.put("shipping", shippingParams);
    Order order = Order.create(params);
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    const stripe = require('stripe')('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
    
    const order = stripe.orders.create({
      currency: 'usd',
      email: 'jenny.rosen@example.com',
      items: [
        {
          type: 'sku',
          parent: 'sku_FLlHo2wdASn5mO',
          quantity: 2,
        },
      ],
      shipping: {
        name: 'Jenny Rosen',
        address: {
          line1: '1234 Main Street',
          city: 'San Francisco',
          state: 'CA',
          postal_code: '94111',
          country: 'US',
        },
      },
    });
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    stripe.Key = "sk_test_4eC39HqLyjWDarjtT1zdp7dc"
    
    params := &stripe.OrderParams{
        Currency: stripe.String(string(stripe.CurrencyUSD)),
        Email: stripe.String("jenny.rosen@example.com"),
        Items: []*stripe.OrderItemParams{
            &stripe.OrderItemParams{
                Type: stripe.String(string(OrderItemTypeSKU)),
                Parent: stripe.String("sku_FLlHo2wdASn5mO"),
                Quantity: stripe.Int64(2),
            },
        },
        Shipping: &stripe.ShippingParams{
            Name: stripe.String("Jenny Rosen"),
            Address: &stripe.AddressParams{
                Line1: stripe.String("1234 Main Street"),
                City: stripe.String("San Francisco"),
                State: stripe.String("CA"),
                PostalCode: stripe.String("94111"),
                Country: stripe.String("US"),
            },
        },
    }
    ord, _ := order.New(params)
    
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    StripeConfiguration.SetApiKey("sk_test_4eC39HqLyjWDarjtT1zdp7dc");
    
    var options = new OrderCreateOptions {
        Currency = "usd",
        Email = "jenny.rosen@example.com",
        Items = new List<OrderItemOptions> {
        new OrderItemOptions {
                Type = "sku",
                Parent = "sku_FLlHo2wdASn5mO",
                Quantity = 2,
            },
        },
        Shipping = new ShippingOptions {
            Name = "Jenny Rosen",
            Address = new AddressOptions {
                Line1 = "1234 Main Street",
                City = "San Francisco",
                State = "CA",
                PostalCode = "94111",
                Country = "US",
            },
        },
    };
    var service = new OrderService();
    Order order = service.Create(options);
    

    When processing the request, Stripe uses the shipping provider’s API to obtain shipping methods and includes them directly in the response. For this example, the response below includes three USPS shipping methods. Note that the first shipping method has been chosen by default and that the order total reflects the shipping costs in addition to the cost of the SKU (some fields removed for brevity).

    {
      "id": "or_18N3kWDAu10Yox5RuJQQpN69",
      "object": "order",
      "amount": 7559,
      ...
      "items": [
        {
          "object": "order_item",
          ...
        },
        {
          "object": "order_item",
          "amount": 560,
          "currency": "usd",
          "description": "USPS: Priority (1 day delivery)",
          "parent": "rate_d3a94b44b02b48668a1d228a8c4982e6",
          "quantity": null,
          "type": "shipping"
        }
      ],
      ...
      "selected_shipping_method": "rate_d3a94b44b02b48668a1d228a8c4982e6",
      "shipping_methods": [
        {
          "id": "rate_d3a94b44b02b48668a1d228a8c4982e6",
          "amount": 560,
          "currency": "usd",
          "description": "USPS: Priority (1 day delivery)"
        },
        {
          "id": "rate_fc289c1131a94af58e19aa3e724d40c5",
          "amount": 595,
          "currency": "usd",
          "description": "USPS: ParcelSelect (2 day delivery)"
        },
        {
          "id": "rate_c8401086a10049fc9b3a9a37bcbcd5d4",
          "amount": 2066,
          "currency": "usd",
          "description": "USPS: Express (1 day delivery)"
        }
      ],
      ...
    }

    Questions?

    We're always happy to help with code or other questions you might have. Search our documentation, contact support, or connect with our sales team. You can also chat live with other developers in #stripe on freenode.

    Questa pagina è stata utile? Yes No

    Send

    Thank you for helping improve Stripe's documentation. If you need help or have any questions, please consider contacting support.

    On this page