1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Ship&Coの料金APIをつかって、海外発送で最も安いサービスを自動的に取得する

Last updated at Posted at 2020-10-24

この記事では、Ship&CoのAPIを使って、DHL、UPS、FedEX、日本郵便EMSなどを使って海外発送する際に、各運送会社のどのサービスを使えば一番安く配送できるか判別する方法を説明します。

海外発送は配送料が高くなりがちですが、同じ運送会社でも指定するサービスによってその料金が異なります。Ship&CoのAPIには、料金というAPIがあり、出荷のAPIからserviceを抜いたパラメータを渡すことで、パラメータに応じた各serviceの料金を一覧で取得することができます。出荷のAPIを呼ぶ前に料金のAPIを呼ぶことで、常に最安のサービスを使って送り状の印字をすることが自動的にできるようになります。

このフローについては、Ship&CoのAPIの概要図にも表現されていますので、ぜひご参照ください。

1. rates APIを呼び出す準備をする

こちらの記事を基にまずは利用する配送会社の送り状印字のパラメータを作ります。そして、そこからserviceの項目を削除して、https://app.shipandco.com/api/v1/ratesPOSTするリクエストJSONを作成します。

{
    "to_address": {
      "full_name": "John Doe",
      "company": "",
      "email": "",
      "phone": "0901231234",
      "country": "FR",
      "zip": "75002",
      "province": "",
      "city": "Paris",
      "address1": "12 Rue du 4 Septembre",
      "address2": ""
    },
    "from_address": {
      "full_name": "Yamada Taro",
      "company": "World Company",
      "email": "ytaro@worldcompany.com",
      "phone": "08012341234",
      "country": "JP",
      "zip": "6050012",
      "province": "KYOTO",
      "city": "KYOTO SHI",
      "address1": "HIGASHIYAMA KU",
      "address2": "SAIKAISHICHO"
    },
    "products": [
      {
        "name": "T-Shirt",
        "quantity": 2,
        "price": 2980,
        "hs_code": "1234.12",
        "origin_country": "JP"
      }
    ],
    "parcels": [
      {
        "weight": 2000,
        "amount": 1,
        "width": 10,
        "height": 10,
        "depth": 10
      }
    ],
    "customs": {
      "duty_paid": false,
      "content_type": "MERCHANDISE"
    },
    "setup": {
      "carrier": "dhl",
      "currency": "JPY",  /* --- ※1 --- */
      "insurance": 0,
      "ref_number": "123-REF-3456",
      "delivery_note": "Leave the box at the door",
      "discount": 0,
      "signature": false,
      "test": true,
      "return_label": false
    }
}

※1:このcurrencyは、出荷のAPIと同様に、発送する製品の価格(productsprice)に対する通貨です。このAPIで返される料金の通貨ではありませんのでご注意ください。

2. APIを実行して、サービスごとの料金を比較する

上記で作成したリクエストをもとに、APIを実行します。成功すると、下記のようなレスポンスが返されるので、各サービスの料金(※2)を比較します。

[
    {
        "carrier": "custom",
        "service": "custom_standard",
        "currency": "",
        "surcharges": {}
    },
    {
        "carrier": "dhl",
        "service": "dhl_express_0900",
        "currency": "JPY",
        "price": 6106,
        "surcharges": [
            {
                "type": "9:00 PREMIUM",
                "price": 3500
            },
            {
                "type": "EMERGENCY SITUATION",
                "price": 40
            }
        ]
    },
    {
        "carrier": "dhl",
        "service": "dhl_express_1200",
        "currency": "JPY",
        "price": 2851,
        "surcharges": [
            {
                "type": "12:00 PREMIUM",
                "price": 600
            },
            {
                "type": "EMERGENCY SITUATION",
                "price": 40
            }
        ]
    },
    {
        "carrier": "dhl",
        "service": "dhl_express_worldwide",
        "currency": "JPY",
        "price": 2178,
        "surcharges": [
            {
                "type": "EMERGENCY SITUATION",
                "price": 40
            }
        ]
    },
    {
        "carrier": "dhl",
        "service": "dhl_express_jumbo",
        "currency": "JPY",
        "price": 34349,
        "surcharges": []
    },
    {
        "carrier": "fedex",
        "service": "fedex_international_first",
        "currency": "JPY",
        "price": 34452,
        "surcharges": [
            {
                "type": "Peak Surcharge",
                "price": 110
            }
        ]
    },
    {
        "carrier": "fedex",
        "service": "fedex_international_priority",
        "currency": "JPY",
        "price": 2235,
        "surcharges": [
            {
                "type": "Peak Surcharge",
                "price": 110
            }
        ]
    },
    {
        "carrier": "fedex",
        "service": "fedex_international_economy",
        "currency": "JPY",
        "price": 2093,
        "surcharges": [
            {
                "type": "Peak Surcharge",
                "price": 110
            }
        ]
    },
    {
        "carrier": "japanpost",
        "service": "japanpost_ems",
        "currency": "JPY",
        "price": 5000,
        "surcharges": []
    },
    {
        "carrier": "japanpost",
        "service": "japanpost_epacket",
        "currency": "JPY",
        "price": 3065,
        "surcharges": []
    },
    {
        "carrier": "japanpost",
        "service": "japanpost_air_parcel",
        "currency": "JPY",
        "price": 5050,
        "surcharges": []
    },
    {
        "carrier": "japanpost",
        "service": "japanpost_sea_parcel",
        "currency": "JPY",
        "price": 2350,
        "surcharges": []
    },
    {
        "carrier": "japanpost",
        "service": "airmail",
        "currency": "JPY",
        "price": 2760,
        "surcharges": []
    },
    {
        "carrier": "ups",
        "service": "ups_saver",
        "currency": "JPY",
        "price": 3471,
        "surcharges": [
            {
                "type": "Ship To Address Classification is changed from Residential to Commercial"
            }
        ]
    },
    {
        "carrier": "ups",
        "service": "ups_worldwide_express_plus",
        "currency": "JPY",
        "price": 7476,
        "surcharges": [
            {
                "type": "Ship To Address Classification is changed from Residential to Commercial"
            }
        ]
    },
    {
        "carrier": "ups",
        "service": "ups_worldwide_express",
        "currency": "JPY",
        "price": 3676,
        "surcharges": [
            {
                "type": "Ship To Address Classification is changed from Residential to Commercial"
            }
        ]
    },
    {
        "carrier": "ups",
        "service": "ups_worldwide_expedited",
        "currency": "JPY",
        "price": 15558,
        "surcharges": [
            {
                "type": "Ship To Address Classification is changed from Residential to Commercial"
            }
        ]
    }
]

※2:料金は、日本郵便海外以外は、Ship&Coに登録されているアカウントの各運送会社との契約に基づき算出されます。料金の通貨も契約により決定されます(基本的には日本円です)。

3. 最安のサービスを使って発送処理をする

上記のレスポンスで最も安いサービスを選んで、出荷のAPIにそのserviceのパラメータを渡すことで、最安で発送をすることができます。

その他の注意点

  • 料金APIは出荷APIを利用する前提で無料で提供されています。料金APIのみ利用しているとみなされた場合は該当アカウントのアクセストークンが無効になりますので、ご注意ください。
  • この記事を執筆時点では料金APIは海外発送のサービス(運送会社とサービス で「海外」と表記されているもの)のみ対応されています。国内発送についての対応は今しばらくお待ちください。
  • 海外発送については、こちらの記事もご参照ください。
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?