Need help with API keys and HttpService (Bad request error 400)

  1. What do you want to achieve? I want to be able to send a request to an API and return results to use in my game

  2. What is the issue? Everytime i run the code i get error 400 bad request (invalid api key)

  3. What solutions have you tried so far? At first, I figured it’s because im missing the GetAsync X-Api-Key headers but when i added it, it still didnt work. So i looked for nearly every single topic about API keys on here and basically memorized the docs at this point on http service and API keys but i still couldn’t find a solution, I understood from the topics that http error 400 is something wrong in my part of how im writing the code. I’m sure the key i typed is correct because it’s copied directly from the dashboard

  • Here is the Api Website
  • Before you reply, notice this: Api requests settings are enabled in my game settings, HTTP requests are also enabled, the code is in a normal script not a local script and yes, I used a pcall but that won’t fix the issue.

Here is my code:

local httpService = game:GetService("HttpService")

local url = "https://api.api-ninjas.com/v1/facts"
local headers = {
	["X-Api-Key"] = "Obviously can't reveal it, but its that one extremely amazingly long characters of the alphabet and is basically like OiskaoOIGSiodiJaOL"
}

local response

pcall(function()
	
	response = httpService:RequestAsync({
		Url = url,
		Method = "GET",
		Headers = headers,
		
	})
	
	
end)

if response then
	
	print(response)
	
end

Output:

{
                    ["Body"] = "{"error": "Invalid API Key."}",
                    ["Headers"] =  ▶ {...}, --irrelevant stuff, some cache data
                    ["StatusCode"] = 400,
                    ["StatusMessage"] = "Bad Request",
                    ["Success"] = false
                 }

Description of the Api key settings:

name X-Api-Key
Status active
Enable key yes
Access permissions → 1. Universe (im sure its the correct experience)
experience operations universe.place.write, universe:write
Security I wrote a correct IP address, for security reasons I cannot show
Expiration No expiration

I hope this is sufficient info. if you need more details, ask me. Any help is appeciated

I tested thesame script in a test place with my own api key and it seems to be working fine, are you sure your api key is correct?

yeah i copied and pasted it directly from the dashboard. pretty sure it wasn’t wrong

does it print an actual table with a fact or just the error print?

a table with status 200

{
[“Body”] = “[{“fact”: “Rocky Mountain spotted fever is a disease caused by ticks”}]”,
[“Headers”] = :arrow_forward: {…},
[“StatusCode”] = 200,
[“StatusMessage”] = “OK”,
[“Success”] = true
}

thats strange. if you dont mind are your key settings the same as mine? if they aren’t, can u tell me the difference so i can change it?

i dont think i have any settings changed, where can i view my key settings?

Creator dashboard → open cloud → Api keys → a list of your api keys

i just got my key here:

i scrolled down, pressed show api key and copied it over
i cant find any dashboard

oh, I thought you meant the create.roblox.com dashboard. I will check it out and see if it works. I hope it does

you need to provide an api ninjas api key to the X-Api-Key header instead of a roblox api key and it should work

man i feel a little stupid for not thinking of that. thanks man i have been on forums for like 3 hours trying to fix it. have a great day

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.