[Semi-Detailed Tutorial] Roblox and API


  • ROBLOX API

    To those who don't know, API stands for Aplication Programming Interface. it is a set of subroutine definitions, protocols, and tools for building application software.
    ROBLOX has a Web API, which is used to connect games or applications to it's online database. (Examples of using this API can be found here.)

    You can use this API for numerous things, from checking if a user is in a guild, to checking if a user owns an asset. With proper authentication, the API can also be used to make account actions remotely,
    things like blocking users or messaging users can be done if given the right credentials. Using this API is pretty straight forward. You simply make an HTTP request, which I will show an example of below.

    Using Post/Get requests to your advantage

    hs = game:GetService("HttpService")
    test = hs:GetAsync("http://example.com", true)
    

    This will perform a 'Get' request, which retrieves information. You can also do a 'Post' request, which requests to post information to the server.

    hs = game:GetService("HttpService")
    myContent = "This_is_a_pointless string that means nothing, but could have useful content."
    hs:PostAsync("http://example.com", myContent)
    

    This can be used like stated above, where you can block or message users remotely (if given the right authorization credentials).

    You can even use it to post to remote online databases.

    myTable = {}
    myTable["exp"] = 1100
    myTable["gold"] = 999
    myTable["swordID"] = 1000398
     
    -- Initialize an HTTP Service pointer
    hs = game:GetService("HttpService")
    -- Encodes the Table into a JSON string.
    myTableJSON = hs:JSONEncode(myTable)
    hs:PostAsync("http://mydatabase.org/json-table", myTableJSON)
    

    This will post the above table (mytable), encoded in JSON, to the database.

    Keep in mind, this is some pretty advanced stuff, and should not be attempted without a good understanding of how HTTP requests, JSON, and API work.
    Below are some links to some helpful sites to help you get started.

    Roblox Wiki: Sending HTTP Requests
    Roblox Web API
    Roblox API

    Roblox's Standard API

    If you head on over to the ROBLOX API web page here, youll notice that before each link, you can see "GET" or "POST".
    This tells you whether you need to POST or GET to utilize the provided data.

    They also use what is called an "API Endpoint", which is http://api.roblox.com. When they say things like "GET /assets/{id}/versions", they mean you need to perform a GET request on, in this specific example,
    http://api.roblox.com/assets/{id}/versions, {id} being replaced with an asset Id. An example of performing this Get request would be like below.

    hs = game:GetService("HttpService")
    local head,body = hs:GetAsync("http://api.roblox.com/assets/192557913/versions", true)
    print(JSONDecode(body))
    

    Sending a request to Roblox will make a trust check failed error, so if you don't have a proxy the above may not work.
    Pretty complicated, right? But, as all things go, it can be easily understood through the Roblox Wiki and asking questions.

    That's all for now, I hope you learned something today!


  • Sending a request to Roblox will make a trust check failed error, so assuming you don't have a proxy,

    hs = game:GetService("HttpService")
    local head,body = hs:GetAsync("http://api.roblox.com/assets/192557913/versions", true)
    print(JSONDecode(body))
    

    is a bad example.


  • Good point, and thanks for pointing it out. I'll edit and put that disclaimer :thumbsup:

Log in to reply
 

Looks like your connection to Scripting Helpers was lost, please wait while we try to reconnect.