I just wanna start with this by saying: i am like not very good with web development so like yeah it wont be the most optimized thing ever but it’ll work!
Hi! This tutorial will show you how to make basic web API’s for your roblox games. So, first of all.
Why would I use a web API?
There’s many reasons. For example – a bot to rank people if they buy a gamepass. A api to get the store of a game and send it to a roblox server. You can host a website if you want, and hook up a domain to it.
What should I keep in mind?
First of all, you’re probably going to need a VPS for this. Glitch works, although it isn’t always up so I use a paid one. You can even port forward, but that is a huge security risk so I wouldn’t recommend it.
Part 1
First, we’re making all of this in Node.js, so you’ll need that. If you don’t have it, click this.
Installing on Windows or MacOS: Download | Node.js (nodejs.org)
Installing on Linux (most vps’ run on this. run it in the terminal):
sudo apt update
sudo apt install nodejs
You’re going to need a code editor too. I use VS Code, and that’s what I’ll be using in the tutorial. So, start by opening up VS Code. You should see something like this:
Now, open up a folder (or make a new one.)
If you see this message, just click the blue button.
Now, we need to initialize our project. At the top, there should be a Terminal button. Click that, and then New Terminal. Now, type
npm init
And press enter 11-12 times. Also, we’re going to need a package for our web server. Type this in the terminal, and press enter:
npm i express
Now that we have it all setup, add a new file. We’ll name this server.js
Open up your new file. Now, we gotta put a few things in there.
var express = require("express")
var app = express()
var port = 2000
app.use(express.json())
app.listen(port)
But, what does this do. First, var defines a variable, just like local defines one in lua. Require is pretty similar to require in roblox. Because we installed express, we can now require that module and use its functions. But why do we call express? Calling express creates a new web server, which is what we want. Lastly, port is just what port it’ll run on. For example, port 80 would be localhost:80. We make it listen for that port, so if you go to localhost:80 in your browser it’ll show up what we have. For this tutorial, we’ll be making a proxy for the roblox API. Kind of like rprxy, but it’s self-hosted (so most times it will be faster!). Also, the app.use(express.json()) makes it so our web API will accept JSON.
To do this, we’re going to make a post request. So, we’re going to add this.
app.post("/ProxyGet", function(Request, Res){
})
What this does, is creates a new page in our api. (Web browsers cannot access it, because they make a GET request, not a POST request. When this is posted to, it runs the callback function we made, with two arguments. The request, and the Response. The response is to send what they want back, and the Request is to get what they want. Here is our code now:
var express = require("express")
var app = express()
var port = 2000
app.use(express.json())
app.post("/ProxyGet", function(Request, Res){
})
app.listen(port)
Now, we’re going to need another module for this, called “request”. To install this, type this in the terminal and press enter:
npm i request
Now, at the start of the code put this:
var request = require("request")
All this does it gets the request module, so we can use it to make an http request. This is our code now:
var express = require("express")
var request = require("request")
var app = express()
var port = 2000
app.use(express.json())
app.post("/ProxyGet", function(Request, Res){
})
app.listen(port)
Inside of our ProxyGet function, we’re going to put this.
app.post("/ProxyGet", function(Request, Res){
if (!Request.body.url){
Res.send("No url provided")
return
}
var URL = Request.body.url
})
What this does, is checks if there is a url to make a request to. ! in if statements means if there is not, so we are checking if url is in the body. If not, we send our response, which is that there was No url provided, and then return (cancel the function). If there is a URL, we define that. Here is our new code:
var express = require("express")
var request = require("request")
var app = express()
var port = 2000
app.use(express.json())
app.post("/ProxyGet", function(Request, Res){
if (!Request.body.url){
Res.send("No url provided")
return
}
var URL = Request.body.url
})
app.listen(port)
With our URL, we can now make the request. This is pretty simple. To do this, add this code:
app.post("/ProxyGet", function(Request, Res){
if (!Request.body.url){
Res.send("No url provided")
return
}
var URL = Request.body.url
request(URL).pipe(Res)
})
Great! What this does, is makes a GET request to the URL, and whatever it gets, it pipes it to the user. Basically, it asks something to the server, the server says something back, and we it tells it back to you. Here is all of our code:
var express = require("express")
var request = require("request")
var app = express()
var port = 80
app.use(express.json())
app.post("/ProxyGet", function(Request, Res){
if (!Request.body.url){
Res.send("No url provided")
return
}
var URL = Request.body.url
request(URL).pipe(Res)
})
app.listen(port)
Now that we have all of this done, let’s get it up and running.
Part 2
For this tutorial, I will be hosting it with DigitalOcean. This should work with other server hosts that can run Ubuntu. First, make sure you’ve saved your work. In visual studio, do CTRL + S. Now, we’re going to need three things.
First of all, open up puttygen. Click generate, and then move your mouse around until the green bar is gone.
Now, copy your public key onto your clipboard.
With that done, you should save your private key somewhere safe.
Now, let’s start our server. In digital ocean, click this:
Enter all the info, like so
Click skip for now.
Now, make a droplet.
Make sure its on Ubuntu. Here, I would choose the cheapest plan (cuz a proxy doesn’t need much power)
When you see this, click SSH Key.
Under that, click New SSH Key
Paste in your key:
And click Add SSH Key. Select your key (if it isn’t selected), and then go down.
Click this:
And wait for it to create. Now that it’s all set up, click on your droplet and copy the IP
Now, open up putty. Paste your ip in this box:
Now, click the plus next to SSH, and then Auth
Now, click the Browse button, and look for the key you generated.
Now that you’re all ready, click Open.
Click Yes if this pops up.
Type root and press enter
Now, we’ll need to install two things. Type these comamnds
sudo apt update
sudo apt install nodejs
sudo apt install npm
npm install pm2 -g
sudo apt install nginx
sudo ufw allow 'Nginx HTTP'
If it asks you to type Y/n during any of the commands, just type y and press enter. With all of this done, your web server is up and running! But you aren’t fully done. Open up FileZilla. When you’re in, click File at the top, then Site Manager.
At the bottom, click New site.
Look for this dropdown, and click it.
Then, click SFTP
With that selected, go back to DigitalOcean and copy the ip again. Paste it into the Host box.
Change the Logon Type to Key file
Type root in the User box. Next to key file, click Browse.
Select the key file you generated
With all of that done, click Connect. Now, open up File Explorer, and look for the Folder you made for your Node.js server. Open it up. Now, in FileZilla, change Remote site to /
Now, we’re going to make a folder. Scroll all the way down and right click on an empty area.
Click create directory. Name it server, and press ok
It’ll make a new directory. Look for it, and double click it. You should see a blank white screen. Now, open up File Explorer again, and select all your files. Drag em in there.
It’ll take a while. Leave your computer until this says 0 or has no number
Yay! It’s all done. Change your remote site to /etc/nginx/sites-available
Now, right click on default and click Download
Copy the local site onto your clipboard
Then, go to it in file explorer. Look for a file called default
Now, right click it and click Open with
Click Visual Studio Code and then OK. In visual studio code, do CTRL + A, then backspace. Now, paste this:
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
# Default server configuration
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.nginx-debian.html;
server_name hellonode;
location ^~ /assets/ {
gzip_static on;
expires 12h;
add_header Cache-Control public;
}
location / {
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://localhost:2000;
allow all;
}
}
With all of this done, you can close VS Code. Go back to FileZilla. Open up File Explorer, and drag default into it.
Click OK, and then you should be good. In putty, type
systemctl restart nginx
Now, let’s get your code on here. In putty, type
cd /server
pm2 start server.js
And your server should be up and running! Try going to the ip in your web browser now. If you see this, it works!
Part 3
Make a new ModuleScript, and put this inside of it. Replace urlhere with your proxies url.
local HttpService = game:GetService("HttpService")
local Url = "http://urlhere/ProxyGet"
local Main = {}
function Main:GetAsync(myUrl)
return HttpService:RequestAsync({
Url = Url,
Body = HttpService:JSONEncode({url = myUrl}),
Headers = {
["Content-Type"] = "application/json"
},
Method = "POST"
}).Body
end
return Main
Now, put that modulescript inside your script, and you can use it like so:
local module = require(script.ModuleScript)
print(module:GetAsync("https://google.com"))
Please note, all url’s must start with http:// or https:// or it will fail.
Conclusion
So yeah that works I guess so pretty cool. Lmk if u want something else. I think this was my first tutorial cant remember