How to set up Swordbattle.io V2 API locally

Introduction

V2 introduces a new API which handles database, accounts, etc. It’s located in the /api folder of the official Github repo.

It does require some effort to get it running locally so I will make a quick but hopefully easy to understand tutorial here.

If you have any issues please just click here to make a new support ticket. We’ll try to respond ASAP.

I’m assuming you have the V2 codebase already setup with server/client running. If not, stay tuned for a future tutorial on how to setup V2 from scratch.

Step One: Install Postgres DB

Download the right version for your computer here. I recommend using the installers provided by EDB (it’s also what I will be using throughout this tutorial). You can visit the EDB Postgres download page here. I will be using the latest version at the time of writing which is v16. You should be able to use a newer version though without much issues (if you do face any use the above link).

Just enable all the options

Password can be your choice, everything else will be default (port 5433/5432 ,etc)

Once it’s done, open up SQL Shell (psql) (in Mac it’s in Applications/PostgreSQL 16)

It should open up like a command prompt or terminal
image

Here login to the database (press enter for everything except the password which you have to type in)

It should show up like postgres=#. If that worked congrats! You have installed the DB successfully.

Step Two: Linking code to API

In your /api folder in the Swordbattle V2 code, make a new file called .env and put the following contents:

# PostgreSQL Connection Details

# Username for the database
DB_USERNAME=postgres

# Password for the database
DB_PASSWORD=my_password

# Hostname or IP Address for the database
DB_HOST=localhost

# Port number for the database (change if 5432)
DB_PORT=5433

# Alternative database URL (optional, only use if you need a complete different URL)
# DB_URL=your_alternative_url_here

Replace the Password with your password, other than that everything should be the same.

Let’s try running it now. In a shell with directory set to your /api folder, install the API dependencies with the command:

yarn install

Once it installed, run the command to start the server:

yarn start

If it worked correctly, you will see a terminal similar to this with a bunch of sql queries at the beginning, everything mapping and the server starting at the end.

If you get an error or it doesn’t say Server is running at the end, make a new ticket by using the top link.

Step Three: Linking client to API.

Finally let’s look at how we can the client to run with the newly created API. First of all figure out where the API is hosted. Usually it’s hosted at http://localhost:8080/ (visiting it from a browser should give you a message like:

{"message":"Cannot GET /","error":"Not Found","statusCode":404}

This means it’s working correctly.

In the /client folder of the code, create a .env file and put the following content in it:

REACT_APP_API=localhost:8080

If you have a different API url, put that instead (no protocols or slashes.)

Then, as you would normally do run all 3 development servers and (client,server, and api.)

Now visit the game as you would normally do (typically client is at http://localhost:3000/) and you should be able to use any API feature (eg. leaderboards, accounts, skin, etc!)

Conclusion

Hope this tutorial helped set up the V2 API on your local machine. Like said before, feel free to raise a ticket whenever you face an issue since there is a high chance someone else is also facing the same issue!

Thanks

6 Likes

Disclaimer: V2 codebase is not public yet so nobody can really do this yet, but it will make sense once the codebase is public.

8 Likes

ofwrinf9wqubounfeufife0frj v2 is very soon.

2 Likes

OH GOODNESS v2 is soon :eyes:

I thought v2 was going to use MongoDb @gautam

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