Skip to main content

Endpoint Introduction

What is Endpoints?

Endpoint is like http endpoint but more strict. It will handle response and response from your game client. You can defind paramers and write custom logic for endpoints.

Create and use Endpoint

  1. Select Endpoint on the sidebar menu on the left. Endpoint Then click on New endpoint button on the top right.

  2. Input the endpoint name and description then click Create button New endpoint

  3. You will see endpoint on the endpoint list Endpoint list

Setup endpoint's parameters

  1. Setup endpoint by add 2 paramers

    NameType
    actionnumber
    messagestring

    Endpoint Params Click save to finish the changes.

Write code to handle endpoint

  1. Let's write some code. Click on Open in Code Editor link. Open Code editor It will bring you the Code Editor tab and open a folder linked to the endpoint. Endpoint Code editor

  2. Edit the file to following code

    import { Request, Response } from "gamedrive";

    export default async function (request: Request, response: Response) {
    try {
    const action = request.args.action;
    if (action == 1) {
    const resultObject = {
    incomingMessage: request.args.message,
    fromPlayerId: request.playerId,
    };
    response.send(resultObject);
    } else if (action == 0) {
    response.sendError({
    code: "ERROR_BY_ACTION",
    message: request.args.message,
    });
    } else {
    throw "Unable to handle action :" + action;
    }
    } catch (error) {
    response.sendError(error);
    }
    }

    Save by using shortkey Crtl + S or Choose top menu File->Save Save code editor Our endpoint logic is now ready to be tested. Since endpoints are for your game players to call. We need a player id to send test request.

Test endpoint

  1. Go to Players menu on sidebar Save code editor Press the Query button and you will see test player data appear.

    Copy the _id field of the player to use on next step

  2. Go back to the endpoint page. We will test the endpoint by using test section.

    Paste the player id to test Request as ... field. input 1 to action field. input "hello from a player" to message field

    NameValue
    action1
    messagehello from a player
    Request as[copied player id from step 7.]

    Input test fields

  3. After input all required data, press the Request button Response data You will see the response made from your game logic

  4. Now let change the action field to 0 or other value you will get error response from the endpoint Response error