How to use HTTPService | Beginner's guide

How to use HTTPService | Beginner’s Guide

Introduction
HTTPService allows scripts to fetch information from external sites. You can use it to create a live group member count, a Roblox stock price display, and a lot of other cool stuff! In this tutorial, we’ll be making a script that gets avatar hats using HttpService.

Example Game (This is copy-locked, read the tutorial to find how to make it!)

Step 1
First thing we want to do is create a new game, publish it, and turn on Http Requests. If you don’t, our script won’t work, and all your time will be wasted. Of course, you can turn it on later if you wish.
Step 2
Next, we want to create our script. Insert a script into ServerScriptService and name it CatalogScript.
Step 3
Let’s code! So first we will define our variables, including the service itself and the actual domain.

local http = game:GetService("HttpService")
local url = "catalog.roproxy.com/" -- We'll add more later

You’ll notice that we are using RoProxy by @okfalse instead of the default roblox.com url. Why is this? Because Roblox doesn’t allow get requests for their own apis. Instead, you have to use a proxy like RoProxy. The proxy we’re using is fast and free, so let’s use it.
Step 4
Now we want to actually get the catalog items from the url. We can sort the results by using the API parameters, provided by Roblox. After inputting these parameters, we get a url like this:

local url = "https://catalog.roproxy.com/v1/search/items/details?Category=11&Subcategory=9"

This url will return all the hat accessories in the Roblox catalog.
Step 5
Now it’s time to code the actual system that will get the hats!

local finished = false
local RunTimes = 0
local MAX_RUN = 2
	
repeat
	local response = http:GetAsync(url)
	local data = http:JSONDecode(response)
	local nextPageCursor = data.nextPageCursor
	local realdata = data["data"]
    print(realdata)
	if nextPageCursor then
		url = "https://catalog.roproxy.com/v1/search/items/details?Category=11&Subcategory=9&Cursor="..nextPageCursor
		RunTimes += 1
	else
		finished = true
	end
until finished == true or RunTimes == MAX_RUN

Code Explanation
So let’s dissect the code and find what each part is doing.

local response = http:GetAsync(url)
local data = http:JSONDecode(response)

This part sends a GET request to the url provided. Then the url response is converted to a nice table.

local nextPageCursor = data.nextPageCursor

There are multiple pages of data, and this is finding the next page.

local realdata = data["data"]

In our data table, there is actually another table which has all the items. We are getting that table here.

The rest of the code is just repeating this loop for every page until there are no more pages or the max page limit; defined as MAX_RUN; has been reached.
Conclusion
In our script, we only print out the table of all the avatar items, but you could send this data to the client and make a nice UI showing all the different avatar items. It’s pretty easy.

That’s about it for this tutorial, thanks for reading!

Entire Script Code
local http = game:GetService("HttpService")
local url = "https://catalog.roproxy.com/v1/search/items/details?Category=11&Subcategory=9"

local finished = false
local RunTimes = 0
local MAX_RUN = 2
	
repeat
	local response = http:GetAsync(url)
	local data = http:JSONDecode(response)
	local nextPageCursor = data.nextPageCursor
	local realdata = data["data"]
    print(realdata)
	if nextPageCursor then
		url = "https://catalog.roproxy.com/v1/search/items/details?Category=11&Subcategory=9&Cursor="..nextPageCursor
		RunTimes += 1
	else
		finished = true
	end
until finished == true or RunTimes == MAX_RUN
20 Likes

Very cool tutorial, however I recommend wrapping your GetAsync call in a pcall to keep the script running if it errors:

local HttpService = game:GetService("HttpService")

local success, res  = pcall(function()
  HttpService:GetAsync(url)
end

if not success then
  print(res)
end
9 Likes