{"message":"API KEY missing from x-api-key header"} in Xcode

Hello all,

I am running into some strange issues with a few GET requests when trying to use them in Xcode.

Here’s an example:

func getAllNFTs(contractAddress:String,completion:@escaping (Int) -> ()) {
        let url = URL(string: "https://deep-index.moralis.io/api/v2//nft/0x3686b85EBA862553546bB8735f225008cc597238?chain=mumbai&format=decimal")
        var request = URLRequest(url: url!)
        request.httpMethod = "GET"
        request.setValue("application/json", forHTTPHeaderField: "accept")
        request.setValue("My-API-Key", forHTTPHeaderField: "X-API-Key")
        URLSession.shared.dataTask(with: request) {(data, response, error) in
            if let responseData = try? JSONDecoder().decode(GetAllNFTResponse.self, from: data!) {
                let total = responseData.total
                print("\(total)")
            }
        }
        .resume()
    }

When I run the function, I’m met with the error “{“message”:“API KEY missing from x-api-key header”}” in the console.

The equivalent curl request works just fine:

curl -X 'GET' \
  'https://deep-index.moralis.io/api/v2/nft/0x3686b85EBA862553546bB8735f225008cc597238?chain=mumbai&format=decimal' \
  -H 'accept: application/json' \
  -H 'X-API-Key: My-API-Key'

I can run POST requests and pass the through to a view without a problem.:

Has anyone else run into this?

It looks like here is my api key instead of x api key

Well yeah, I just used “My-API-Key” as the value for the post instead of including the API key itself.

I mean the key value, not the value of the api key

Ohh - in Xcode you put them in backwards; you declare the value of the header and then the header field:

This…

request.setValue("application/json", forHTTPHeaderField: "accept")
request.setValue("apiKeyhere", forHTTPHeaderField: "X-API-Key")

…is equivalent to this:

-H 'accept: application/json'
-H 'X-API-Key: apiKeyhere'

I have no idea - especially if POST requests work fine -_-

ok… I din’t know that, can you print the entire http header for the request that you make and see if everything looks ok there?

Sure thing. URL is:

"https://deep-index.moralis.io/api/v2/nft/0x3686b85EBA862553546bB8735f225008cc597238?chain=mumbai&format=decimal"

The URLRequest creation looks like:

let url = URL(string: "https://deep-index.moralis.io/api/v2/nft/0x3686b85EBA862553546bB8735f225008cc597238?chain=mumbai&format=decimal")
var request = URLRequest(url: url!)
request.httpMethod = "GET"
request.setValue("application/json", forHTTPHeaderField: "accept")
request.setValue("apiKeyhere", forHTTPHeaderField: "X-API-Key")

And the result Xcode:

why is api key twice there?

It’s displaying the info as an array, then lets you arrow down to see the rest in more detail. It’s normally presented collapsed:

It’s gotta be an Xcode problem. Everywhere else works.

can you see the final http request that is sent somehow?

My apologies for not seeing this. I figured it out - I had to get a new API key.

I had this issue with another site’s API and a new key did the trick there, too.

1 Like