Trunc.us API

All about simplicity...

To request a truncation, POST to /

    {"format": "json", "url": "http://example.com"}
    

Truncation response:

    {"trunct":{"token": "3bU65R", "url": "http://example.com"}}
    

To expand a token, GET /<token>.json

    GET /3bU65R.json
    

Expand response:

    {"trunct":{"token": "3bU65R", "url": "http://example.com"}}
    

Sample Client Library in Ruby

    require 'rubygems'
    require 'json'
    require 'uri'
    require 'net/http'

    class TrunctClient
      attr_accessor :host, :port
      def initialize(options={})
        self.host = options[:host]
        self.port = options[:port] || 80
      end

      def trunc(url)
        req = Net::HTTP::Post.new("/", initheader = {'Content-Type' =>'application/json'})
        req.body = {"url" => url, "format" => "json"}.to_json
        response = Net::HTTP.new(host, port).start {|http| http.request(req) }
        data = JSON::parse(response.body)
        data["trunct"]["token"]
      end

      def expand(token)
        url = URI.parse("http://#{host}/#{token}.json")
        req = Net::HTTP::Get.new(url.path)
        res = Net::HTTP.start(host, port) {|http|
          http.request(req)
        }
        data = JSON::parse(res.body)
        data["trunct"]["url"]
      end

    end
    

And that's it.