DhlFreightApi
A Ruby gem wrapper for the DHL Freight API.
Installation
Install the gem and add to the application's Gemfile by executing:
bundle add dhl_freight_api
If bundler is not being used to manage dependencies, install the gem by executing:
gem install dhl_freight_api
Usage
Initialize the client with your DHL Freight client-key:
client = DhlFreightApi::Client.new(
"YOUR_CLIENT_KEY",
prod: false # false => test, true => production
)
Environment Domains
prod: falseuseshttps://test-api.freight-logistics.dhl.comprod: trueuseshttps://api.freight-logistics.dhl.com
Error Handling
API failures raise DhlFreightApi::Error.
begin
client.shipment_booking.create(payload)
rescue DhlFreightApi::Error => error
puts error.
puts error.response_http_code
puts error.response_body
end
The wrapper raises on:
- non-2xx HTTP responses
- business validation responses with
status: "Error"from ShipmentBooking (even when HTTP is200)
Shipment Booking
Create a shipment booking:
payload = {
"id" => "",
"productCode" => "107",
"totalNumberOfPieces" => 1,
"totalWeight" => 10,
"totalVolume" => 0.05,
"references" => [{ "qualifier" => "CU", "value" => "Elin000123" }],
"payerCode" => { "code" => "001" },
"parties" => [
{
"id" => "111111",
"type" => "Consignee",
"name" => "Consignor Company AB",
"address" => {
"street" => "Gustav III:s Boulevard 18",
"cityName" => "Solna",
"postalCode" => "16973",
"countryCode" => "SE"
},
"phone" => "+46701234567",
"email" => "[email protected]"
}
],
"pieces" => [
{
"packageType" => "PKT",
"numberOfPieces" => 1,
"weight" => 10,
"volume" => 0.05,
"length" => 30,
"width" => 20,
"height" => 15
}
]
}
booking = client.shipment_booking.create(payload)
puts booking.status
puts booking.transport_instruction.id
puts booking.raw
Print Documents
Request print documents by shipment IDs:
payload = {
"shipmentIds" => ["2906221086"],
"options" => {
"label" => true,
"shipmentList" => false,
"waybill" => false,
"guarantee" => false,
"returnLabel" => false,
"licensePlateBarCode" => "GS1",
"pageOptions" => {
"pageType" => "Label",
"marginLeft" => 0,
"marginTop" => 0,
"padding" => 0
}
}
}
print_result = client.print.documents_by_id(payload)
# Raw base64 string is preserved as-is
base64_pdf = print_result.reports.first.content
puts base64_pdf[0..30]
To decode outside the wrapper:
require "base64"
binary_pdf = Base64.decode64(base64_pdf)
File.binwrite("label.pdf", binary_pdf)
Development
After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/andychong/dhl_freight_api. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the DhlFreightApi project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.