           site = settings.get 'githubs', github.default()
           path = path.substr 1 if path[0] == '/'
           [user, repo, segments...] = path.split '/'
-          url = "#{site.api_base_url}/repos/#{user}/#{repo}/contents/#{segments.join '/'}"
+          url = github.to_api_url site, "/repos/#{user}/#{repo}/contents/#{segments.join '/'}"
         else
           site = github.get_github path
           if path.indexOf(site.api_base_url) == 0
-            url = path
+            url = URI path
           else
             uri = URI path
             path = uri.pathname()
             path = path.substr 1 if path[0] == '/'
             [user, repo, x, ref, segments...] = path.split '/'
-            url = "#{site.api_base_url}/repos/#{user}/#{repo}/contents/#{segments.join '/'}?ref=#{ref}"
+            url = github.to_api_url site, "/repos/#{user}/#{repo}/contents/#{segments.join '/'}", {ref}
 
       save_gist: (gist, options={}) ->
         github_host = options.github ? github.default()
         gh = settings.get 'githubs', github_host
-        http.post "#{gh.api_base_url}/gists?access_token=#{gh.access_token}", gist
+        http.post github.to_api_url(gh, '/gists'), gist
+
+      to_api_url: (instance, path, params={}) ->
+        result = URI("#{instance.api_base_url}#{path}").setQuery(params)
+        result.setQuery('access_token', instance.access_token) if instance.access_token?
+        result
 
       to_gist_url: (gist) ->
         build_url = (site, id) ->
-          url = site.api_base_url + "/gists/#{id}"
-          if site.access_token
-            url += "?access_token=#{site.access_token}"
-          url
+          github.to_api_url site, "/gists/#{id}"
         gist = gist.toString()
         if gist.indexOf('http') != 0
           site = settings.get 'githubs', github.default()

            [id, rest...] = URI(gist).filename().split '.'
            build_url site, id
          else
            gist

    cmd 'load', 'Loads a file from GitHub', (path, options={}) ->
      url = github.to_repo_url path
      ensure_access @, url
      @async ->
        @text "Loading file #{path}"
        promise = github.get_repo_contents(url)
        .then (file) =>
          notebook.handle_file @, file, options
        promise.fail (response) =>
          @error response.statusText
        promise

    ensure_access = (ctx, url) ->
      unless url?
        instance = settings.get 'githubs', github.default()
      else
        instance = github.get_github url
      if instance? and instance.requires_access_token and not instance.access_token?
        result = Q.defer()
        ctx.text 'Please set a GitHub access token:'
        input = ctx.input.text_input()
        button = ctx.input.button('Save')
        button.map(input).onValue (access_token) ->
          domain = URI(url).hostname()
          global_settings.user_settings.set 'github', 'githubs', domain, 'access_token', access_token
          result.resolve()
        result.promise
      else
        Q true

    cmd 'gist', 'Loads a script from a gist', (gist, options={}) ->
      if arguments.length is 0

        url = github.to_gist_url gist
        @async ->
          authorized = ensure_access @, url
          authorized.then (url) =>
            @text "Loading gist #{gist}"
            promise = http.get url
            promise.done (response) =>
