https://github.com/nkezhaya/shippex
Elixir shipping library
https://github.com/nkezhaya/shippex
carrier elixir elixir-lang fetching-rates shipping ups usps
Last synced: 2 months ago
JSON representation
Elixir shipping library
- Host: GitHub
- URL: https://github.com/nkezhaya/shippex
- Owner: nkezhaya
- License: mit
- Created: 2016-12-09T16:12:04.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2023-11-09T08:55:32.000Z (over 2 years ago)
- Last Synced: 2024-04-15T02:46:36.100Z (about 2 years ago)
- Topics: carrier, elixir, elixir-lang, fetching-rates, shipping, ups, usps
- Language: Elixir
- Homepage:
- Size: 629 KB
- Stars: 13
- Watchers: 2
- Forks: 6
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Shippex
[](https://hex.pm/packages/shippex)
[](https://hexdocs.pm/shippex/)
[](https://hex.pm/packages/shippex)
[](https://hex.pm/packages/shippex)
[](https://github.com/whitepaperclip/shippex/commits/master)
Shippex is an abstraction of commonly used features in shipping with various carriers. It provides a (hopefully) pleasant API to work with carrier-provided web interfaces for fetching rates and printing shipping labels.
As of now, only UPS and USPS are supported. More carrier support will come in the future. Units of measurement are mostly hardcoded to inches and miles.
## Installation
Add `shippex` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[{:shippex, "~> 0.18"}]
end
```
Ensure `shippex` is started before your application:
```elixir
def application do
[applications: [:shippex]]
end
```
## Configuration
```elixir
config :shippex,
env: :dev,
distance_unit: :in, # either :in or :cm
weight_unit: :lbs, # either :lbs or :kg
currency: :usd, # :usd, :can, :mxn, :eur
carriers: [
ups: [
username: "MyUsername",
password: "MyPassword",
secret_key: "123123",
shipper: %{
account_number: "AB1234",
name: "My Company",
phone: "123-456-7890",
address: "1234 Foo St",
city: "Foo",
state: "TX",
postal_code: "78999"
}
],
usps: [
username: "MyUsername",
password: "MyPassword"
]
]
```
## Usage
```elixir
# Create origin/destination addresses.
origin = Shippex.Address.new(%{
name: "Earl G",
phone: "123-123-1234",
address: "9999 Hobby Lane",
address_line_2: nil,
city: "Austin",
state: "TX",
postal_code: "78703"
})
destination = Shippex.Address.new(%{
name: "Bar Baz",
phone: "123-123-1234",
address: "1234 Foo Blvd",
address_line_2: nil,
city: "Plano",
state: "TX",
postal_code: "75074",
country: "US" # optional
})
# Create a package. Currently only inches and pounds (lbs) supported.
package = Shippex.Package.new(%{
length: 8,
width: 8,
height: 4,
weight: 5,
description: "Headphones",
monetary_value: 20 # optional
})
# Link the origin, destination, and package with a shipment.
shipment = Shippex.Shipment.new(origin, destination, package)
# Fetch rates to present to the user.
rates = Shippex.fetch_rates(shipment, carriers: :usps)
# Accept one of the services and print the label
{:ok, rate} = Enum.shuffle(rates) |> hd
# Fetch the label. Includes the tracking number and a gif image of the label.
{:ok, transaction} = Shippex.create_transaction(shipment, rate.service)
rate = transaction.rate
label = transaction.label
# Print the price.
IO.puts(rate.price)
# Write the label to disk.
File.write!("#{label.tracking_number}.#{label.format}", Base.decode64!(label.image))
```
## TODO:
Carrier support:
- [x] UPS
- [x] USPS
- [ ] FedEx