Test with Insomnia

Insomnia is a really useful tool to test direct http request before coding them in your client (Unity). So you can test your API server directly before coding the client-side code.

Http requests are usually called automatically by the Unity Client and Unity Server, but if you want to understand a bit more how they work in the background, you can try to run these in insomnia:

Download the Free version of Insomnia

https://insomnia.rest/

Start Insomnia, You can either create a new collection: Create -> Request,

or import mine Create -> Import from file (you can find the file in TcgEngine/Install folder).

Then click on your new collection to open the list of requests.

Register a user

From here you can create request to test your API. Let's try to create a new admin user. You can edit an existing request or create a new one.

POST http://127.0.0.1/users/register

Change the Body to JSON and put this as body:

{
    "username": "admin",
    "email": "admin@test.com",
    "password": "admin"
}

Since we are testing locally, the password/email doesn't matter for now. We just use something easy to remember.

Make sure NodeJS is running in your command line window, and click on SEND

If it works, hopefully you should see something like this.

{
    "success": true,
    "id": "64079a109630dced89a3f612"
}

Login

POST http://127.0.0.1/auth

Change the Body to JSON and put this as body:

{
    "username": "admin",
    "password": "admin"
}

When login is sucessful, it will give you an access_token, you will need it to execute any requests that require authentication. Copy the access token you got (without the ")

Read the list of all users

Let's create a new request to read the list of all users

GET http://127.0.0.1/users

Leave the body empty, instead click on Headers and add one, as header name, write:

authorization

And as value paste the access token you got when login in.

Now click on send, it should show the full list of existing users.\

Read the list of all cards

This request will let you see all the cards uploaded to the API. This request could be useful if you would like to create a website that display all your cards. The image could simply be the id of the card + .png uploaded to a specific folder of your website.

GET http://127.0.0.1/cards

Check who is online

This request will let you see the list of all users who interacted with the API in the last 10 minutes.

GET http://127.0.0.1/online

Granting Permissions

If you also have a game server, you could create a "server" user with permission level 5. This will allow the user to give rewards and read other users, but won't let it assign permissions or delete users (only an admin can).

To set permission_level of a user through requests, if you already have at least 1 admin user, you can login with the admin user and use

POST http://127.0.0.1/users/permission/edit/6407a114953

Replace the ID at the end of the URL by the ID of the user you want to target. Use the authorization token of your admin user. And set the body to:

{
    "permission_level" : 5
}

There are only 4 relevant permission levels in this API. 1 for regular users, 5 for the game server, 10 for admins, and 0 for deactivated users.

Other Request?

Great, now you know how to use Insomnia, you can do the same for all other requests. To know which requests are possible, check the .routes.js files inside the NodeJS code.

Testing request on the cloud server

To test online request, once you installed your cloud server, you can replace 127.0.0.1 by the IP of your server, or your domain name. If you enabled https, you can also replace http by https in the URL. Other than that everything should work the same.

Last updated