CreatePublicLobbyInTokyo

InfoGenerateCreated ByPackages

This JavaScript code snippet is designed to create a new lobby using the Hathora API. It specifically targets the Hathora endpoint for creating lobbies (`/lobby/v3/${appId}/create`) and specifies the following details: the lobby will be publicly visible, it will be named 'helloworld room' for configuration purposes, and it will be located in the Tokyo region. The code constructs a POST request to this endpoint, including headers that specify the content type as JSON and include an authorization token ('Bearer YOUR_PLAYER_AUTH_TOKEN'), which should be replaced with an actual token in a production environment. Upon sending the request, the code handles the response by parsing it into JSON and logging it to the console. If there is an error during the request, it will be caught and logged to the console as well.

// Let's break down the requirements and structure the code accordingly:

const appId = 'oh-hello-there';
const url = `https://api.hathora.dev/lobby/v3/${appId}/create`;

const requestOptions = {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer YOUR_PLAYER_AUTH_TOKEN' // Replace with actual token
  },
  body: JSON.stringify({
    visibility: 'public',
    roomConfig: 'helloworld room', // Optional configuration parameter, used here to name the room
    region: 'Tokyo'
  })
};

fetch(url, requestOptions)
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error('Error:', error));

// This JavaScript code makes a POST request to create a public lobby named "helloworld room" in the region of Tokyo for the specified appId.