How can we improve the API?

Insert API key directly into URL

Most APIs allow for the insertion directly into the URL string. This would make it easy to use in native iOS apps. As it is today, the use of the API key is complex. I see no examples of how to code this for native iOS apps in Swift 3.0

6 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    Steve shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    8 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      Signed in as (Sign out)
      Submitting...
      • Anonymous commented  ·   ·  Flag as inappropriate

        Swift 4:

        let path = "v1/bibles/\(bibleId)/chapters/\(bookId).\(chapterId)?content-type=text&include-notes=false&include-chapter-numbers=false"
        guard let biblesUrl = URL(string: path, relativeTo: URL(string: "https://api.scripture.api.bible")) else { return }
        let config = URLSessionConfiguration.default
        config.httpAdditionalHeaders = ["api-key": self.apiKey()]
        let session = URLSession(configuration: config)
        let task = session.dataTask(with: biblesUrl) { (data: Data?, resp: URLResponse?, err: Error?) in

        if err != nil {
        print("connection error:", err!)
        return
        }
        else if let r = resp as? HTTPURLResponse,
        r.statusCode > 299 {
        print("API error:", r)
        return
        }
        if let json = try? JSONSerialization.jsonObject(with: data!, options: []),
        let dict = json as? [String: Any] {
        }

        }

      • Gary Mansted commented  ·   ·  Flag as inappropriate

        Hi guys, i was looking around for a Swift 3 solution as well and figured this out. To make it super ease -> install the Alamofire library via cocoapods (https://github.com/Alamofire/Alamofire), then just call your request as such:

        func testQuery() {
        Alamofire.request("https://abcdefghijklmnopqrstuvwxyz:@bibles.org/v2/search.js?query=john+3:16&version=eng-GNTD").responseJSON { response in
        print(response)
        if let json = response.result.value {
        print("JSON: \(json)")
        }
        }
        }

        NOTE: Replace: abcdefghijklmnopqurstuvwxyz with your private API key
        NOTE: Make sure you import Alamofire in your class headers.

        Hope this helps someone :)

      • Stu Andrews commented  ·   ·  Flag as inappropriate

        Hmmm, also - The #{} characters can't be in the url, the documentation might need some clarification on that.

        Cheers.

      • Stu Andrews commented  ·   ·  Flag as inappropriate

        Hi Folks,

        Just an FYI - The documentation page is, I think from the below comment from @JohnMarkMitchell, is missing an "@" in the url (for using the api key in a url).

        Doc says:

        https://#{your API token}:bibles.org/v2/versions/eng-GNTD.xml

        But from what John says below it should be:

        https://#{your API token}:@bibles.org/v2/versions/eng-GNTD.xml

        Was having a time trying to get it work until I found this thread :)

        Cheers.

      • Steve commented  ·   ·  Flag as inappropriate

        I've figured this out. My solution was based upon the @Nate Cook's answer in this Stackoverflow answer: http://stackoverflow.com/questions/24379601/how-to-make-an-http-request-basic-auth-in-swift

        The code needed to be updated a bit to Swift 3, and the code calls for a username AND a password. This Bible Search API provides only a API key. The secret for me was figuring out that the API key goes in the username variable and for the password simply use: ""

        The API uses Basic authentication which must send the API key in base64EncodedString. The stackoverflow answer shows how to do that.

        I used URLSession instead of NSURLConnection like so:
        let task = URLSession.shared.dataTask(with: request) { (data, response, error)

        I hope this helps others.

      • Steve commented  ·   ·  Flag as inappropriate

        I've attempted to follow that stackoverflow and a couple of other similar techniques. I keep getting back a 401 status which is an authentication error. I am able to user my token with a static URL like this: https://xYz1234nMUXmytokenhere@bibles.org/v2/search.js?query=john+3:16&version=eng-GNTD However, when the token is inserted as an authorization header as described here it doesn't work: http://swiftdeveloperblog.com/http-get-request-example-in-swift/ It would be great to get some source code from someone who has verified functionality.

      • AdminJohn Mark Mitchell (Admin, American Bible Society) commented  ·   ·  Flag as inappropriate

        For use in Swift, it seems possible to follow the format outlined in this Stackoverflow post: http://stackoverflow.com/questions/24379601/how-to-make-an-http-request-basic-auth-in-swift

        Can you give that a try and let us know if it works for you?

        For others that might pass by this looking for help with other programming languages, the http spec allows the HTTP Auth info to be passed as a header directly OR along on the URL. Fortunately many http libraries/functions properly support the IETF URL spec (unlike many desktop/mobile browsers which no longer support this method). The basic format following the spec is user:pass@example.com

        For access to the bible API we only include a username (your API token), so a call to the versions endpoint would look like the following: https://api-token-here:@bibles.org/v2/versions.xml where api-token-here is replaced with your API token.

        For a more in-depth conversation on passing the auth on the URL, I would encourage reading http://serverfault.com/questions/371907/can-you-pass-user-pass-for-http-basic-authentication-in-url-parameters

      Feedback and Knowledge Base