Skip to content

Instantly share code, notes, and snippets.

@tastycode
Last active November 4, 2019 22:53
Show Gist options
  • Save tastycode/c61b9f7af807d51d2861 to your computer and use it in GitHub Desktop.
Save tastycode/c61b9f7af807d51d2861 to your computer and use it in GitHub Desktop.
Automating Shipping Through Shyp

Shyp is awesome. They can pick up what you have to ship in minutes and take care of all the packing and addressing. Their rates are as cheap as you can get and they have a custom boxing process that minimizes void space.

This would be great for shipping things in bulk if it weren't for them not having an API, or any other means to enter shipments other than their iPhone app.

I had 100 shipments to make, so I threw together this script to automate the creation of the shipments. This isn't for the faint of heart, you'll have to know how to create a transparent HTTPS proxy and hook your iPhone up to it so you can get your session and authorization headers. I'm not going to go into that, but once you get the appropriate headers, put them in a file called shyp.headers. You'll also need to create a shipment manually through the app in order to get a PICKUP_ID. Once you have that, just add your addresses to the array called addresses and run the script. I also didn't want to figure out how to do multipart file uploads from this script, so I just reused the same photo-id over and over again, don't forget to replace it!

This will break as soon as they make the slightest change to their API. Good luck!

#!/usr/bin/env ruby
require 'json'
require 'uri'
require 'pry'
PICKUP_ID = "pik_d2e2c008-f77f-436b-a808-6cb27d7cf5af"

def test_connection
  res = %x{curl -H "$(cat shyp.headers)" 'https://api.shyp.com/users/usr_91512728-6667-47a1-8dbf-0586ddae1bb4/shipping-history?limit=20&skip=4'}
  JSON.parse(res)
end

def create_item(pickup_id)
  JSON.parse(%x{curl -X POST -H "$(cat shyp.headers)" https://api.shyp.com/pickups/#{pickup_id}/items})
end

def add_address(item_id, name, line1, line2, city, state, zip)
  address_hash = {
    addressLine1: line1,
    addressLine2: line2,
    city: city,
    region: state,
    postCode: zip,
    lat: 0,
    lng: 0,
    country: "United States",
    countryCode: "US",
    contactName: name,
    phoneNumber: ""
  }

  address_data = address_hash.map do |k, v|
    "#{k}=#{URI.escape(v.to_s)}"
  end.join("&")

  puts address_data
  JSON.parse(%x{curl -X POST -H "$(cat shyp.headers)" -d '#{address_data}' https://api.shyp.com/pickups/#{PICKUP_ID}/items/#{item_id}/address})
end

def add_photo(item_id)
  photo_id = "pho_84ee84aa-2ff7-49ff-b9f4-4aec024ffe3b"
  JSON.parse(%x{curl -X POST -H "$(cat shyp.headers)" -d 'photoId=#{photo_id}' https://api.shyp.com/pickups/#{PICKUP_ID}/items/#{item_id}/photo})
end

addresses = %Q{
Satya Devol|115 Valencia|Apt 2|San Francisco|California|94133
}.lines[1..-1]

addresses.each do |address_line|
  name, line1, line2, city, state, zip = address_line.split("|").map(&:strip)
  p zip
  item = create_item(PICKUP_ID)
  puts "Item created #{item["id"]}"
  item_id = item["id"]
  puts "Adding Address"
  p add_address(item_id, name, line1, line2, city, state, zip)
  puts "Adding Photo"
  p add_photo(item_id)
end
@DrPumpkin
Copy link

No wonder Shyp went out of business last year, they ignored great ideas like this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment