Unsure on how to use this API?

I’ve recently started playing around with HTTPSservice and it doesn’t seem to be working the way I want it to.
I’ve been trying to get a random fact from this specific link (API)
http://api.fungenerators.com
But it doesn’t return a fact, it returns something totally random:

I’m unsure what I’ve done wrong. My code is this:

local randomFactsURL = "http://api.fungenerators.com"
local httpService = game:GetService("HttpService")
local function FetchFacts()
	local response 
	local success, errormessage = pcall(function()
		response = httpService:RequestAsync(
			{
				Url = randomFactsURL,
				Method = "GET",
				Headers = {
					["Content-Type"] = "application/json"
				}

			}
		)
	end)
	if success then
		if response.Success then
			print(response.Body)
		end
	else
		warn(errormessage)
	end
end

FetchFacts()

Thanks in advance :slight_smile:

1 Like

Just looking at the API itself you need to pass some parameters like seen in this screenshot:
image
Another this is that I’m pretty sure you need a API token to actually use their API so make sure you do that as well.

1 Like

Yeah ok. I was a bit stuck because I didn’t know what Curl-X was, so it was very confusing trying to implement that on Studio

http://api.fungenerators.com/fact/random?category=TheCategory&subcategory=TheSubCategory?X-Fungenerators-Api-Secret=YourKey

Or

http://api.fungenerators.com/fact/random?category=TheCategory&subcategory=TheSubCategory?api_key=YourKey

1 Like

Ok thank you that’s very helpful. I’ll try it out and mark your answer as a solution if it works

The thing is, nothing is being returned when I try it. My current code is:

local randomFactsURL = "http://api.fungenerators.com/fact/random?category=Countries&subcategory=USA?X-Fungenerators-Api-Secret=444777"
local httpService = game:GetService("HttpService")
local function FetchFacts()
	local response 
	local success, errormessage = pcall(function()
		response = httpService:RequestAsync(
			{
				Url = randomFactsURL,
				Method = "GET",
				Headers = {
					["Content-Type"] = "application/json",
					}

			}
		)
	end)
	if success then
		if response.Success then
			print(response.Body)
		else
			print("Nothing returned")
		end
	else
		warn(errormessage)
	end
end

FetchFacts()

What do you mean there’s nothing returned? I can’t see a return anywhere. Did you mean the printing?

Yes the printing. Sorry for any confusion caused lol

Do you know the concept of variables inside functions and outside functions? You need to add the if statement inside the function to make it work I guess.

Edit: I do know this is in Python, I do not know the exact concept in Luau.

I’m confused. All my if statements are within the local function if that’s what you mean.

No, the if success then is outside of the function as you can clearly see end. Here’s what you need to do. Add the if else statement inside the function.

1 Like
local randomFactsURL = "http://api.fungenerators.com/fact/random?category=Countries&subcategory=USA?X-Fungenerators-Api-Secret=444777"

local httpService = game:GetService(“HttpService”)
local function FetchFacts()
local response
local success, errormessage = pcall(function()
response = httpService:RequestAsync(
{
Url = randomFactsURL,
Method = “GET”,
Headers = {
[“Content-Type”] = “application/json”,
}
print(response.Body)
else
print(“Nothing returned”)
end
else
warn(errormessage)
}
)
end)
if success then
if response.Success then

end

end

FetchFacts()

Sorry about the indentation, it literally broke.

1 Like

Did it work? If it didn’t I’ll take a look at the API.

I’m reopening the project rn. So I’ll test it soon

Also be sure to indent it correctly lol.

Uhm. It messed up a lot. I’m getting errors about ends. I didn’t get these previously

What do you mean? Wait a second I’ll try it.

Oh I see what’s happening. HttpService doesn’t work in LocalScripts.

It’s not in a local script though. It’s a server script

Oh, and also I see what I’ve done. I completely messed up the script. I am going to rewrite the script to see if it works.

1 Like