Overview
The MyMCLib
library provides a convenient way to interact with the My-MC.Link API for managing Minecraft servers. It includes methods for performing various actions such as retrieving server information, managing server settings, controlling gameplay, and interacting with players.
Installation
To use the MyMCLib
library in your project, install it via npm:
npm install mymc-lib
Proper Importing: To make use of this module, you'll need to either utilize the .mjs
extension or incorporate "type": "module"
in your package.json
file. This requirement exists because modern JavaScript (ES6 and later) introduced the concept of modules, which allows for more organized and scalable code by encapsulating functionality within separate files.
Usage
First, import the library into your project:
import MyMCLib from "mymc-lib";
Then, create an instance of MyMCLib
by providing your API token:
const MyMC = new MyMCLib("your-api-token");
Example Usage
import MyMCLib from "mymc-lib";
// Instantiate MyMCLib with your API token
const MyMC = new MyMCLib("your-api-token");
// Will print information about API key expiration date
let keyData = await MyMC.getTime();
console.log(keyData);
Output:
root@test:/home# node test
{
success: true,
keyexpireString: 'Tue Apr 15 2025 06:14:59 GMT-0400 (Eastern Daylight Time)',
expireDate: '2025-04-15T10:14:59.000Z',
expireEpoch: 1744712099000
}
Code Examples
Getting Server Time
const timeData = await MyMC.getTime();
console.log(timeData);
Getting Server Statistics
const statsData = await MyMC.getStats();
console.log(statsData);
Getting Server Logs
const logData = await MyMC.getLog();
console.log(logData);
Starting the Minecraft Server
const startResult = await MyMC.startServer();
console.log(startResult);
Stopping the Minecraft Server
const stopResult = await MyMC.stopServer();
console.log(stopResult);
Restarting the Minecraft Server
const restartResult = await MyMC.restartServer();
console.log(restartResult);
Creating a Custom Link
const customLinkResult = await MyMC.createMyLink();
console.log(customLinkResult);
Deleting a Custom Link
const deleteLinkResult = await MyMC.deleteMyLink();
console.log(deleteLinkResult);
Creating an SFTP Link
const sftpLinkResult = await MyMC.createLinkSFTP();
console.log(sftpLinkResult);
Deleting an SFTP Link
const deleteSftpLinkResult = await MyMC.deleteLinkSFTP();
console.log(deleteSftpLinkResult);
Getting Connection Hash
const hashData = await MyMC.getConnectionHash();
console.log(hashData);
Getting SFTP Connection Hash
const sftpHashData = await MyMC.getConnectionHashSFTP();
console.log(sftpHashData);
Getting Online Players
const playersData = await MyMC.getOnlinePlayers();
console.log(playersData);
Getting Server's Website URL
const websiteURL = await MyMC.getWebsiteURL();
console.log(websiteURL);
Getting Server's Map URL
const mapURL = await MyMC.getMapURL();
console.log(mapURL);
Banning a Player
const banResult = await MyMC.postBan("username");
console.log(banResult);
Unbanning a Player
const unbanResult = await MyMC.postUnban("username");
console.log(unbanResult);
Sending a Message to All Players
const sayResult = await MyMC.postSay("Hello, everyone!");
console.log(sayResult);
Sending a Private Message to a Player
const tellResult = await MyMC.postTell("username", "Hey, how are you?");
console.log(tellResult);
Executing a Command in Server Console
const consoleResult = await MyMC.postConsole("say Hello, world!");
console.log(consoleResult);
Giving an Item to a Player
const giveResult = await MyMC.postGive("username", "item", 1);
console.log(giveResult);
Installing a Mod by ID
const modID = "exampleMod123";
const installResult = await MyMC.installMod(modID);
console.log(installResult);
Uninstalling a Mod by ID
const modID = "exampleMod123";
const uninstallResult = await MyMC.uninstallMod(modID);
console.log(uninstallResult);
Retrieving a List of Installed Mods
const installedMods = await MyMC.getInstalledMods();
console.log(installedMods);
API Reference
MyMCLib Class
Constructor
constructor(token: string)
Creates an instance of the MyMCLib
class with the provided API token.
Methods
getTime(): Promise
- Retrieves the current server time.getStats(): Promise
- Retrieves server statistics.getLog(): Promise
- Retrieves server logs.startServer(): Promise
- Starts the Minecraft server.stopServer(): Promise
- Stops the Minecraft server.restartServer(): Promise
- Restarts the Minecraft server.createMyLink(): Promise
- Creates a custom link.deleteMyLink(): Promise
- Deletes a custom link.createLinkSFTP(): Promise
- Creates an SFTP link.deleteLinkSFTP(): Promise
- Deletes an SFTP link.getConnectionHash(): Promise
- Retrieves a connection hash.getConnectionHashSFTP(): Promise
- Retrieves an SFTP connection hash.getOnlinePlayers(): Promise
- Retrieves a list of online players.getWebsiteURL(): Promise
- Retrieves the server's website URL.getMapURL(): Promise
- Retrieves the server's map URL.postBan(username: string): Promise
- Bans a player by username.postUnban(username: string): Promise
- Unbans a player by username.postSay(message: string): Promise
- Sends a message to all players.postTell(username: string, message: string): Promise
- Sends a private message to a player.postConsole(command: string): Promise
- Executes a command in the server console.postGive(username: string, item: string, amount: number): Promise
- Gives an item to a player.installMod(modID: string): Promise
- Installs a mod by ID.uninstallMod(modID: string): Promise
- Uninstalls a mod by ID.getInstalledMods(): Promise
- Retrieves a list of installed mods.
ApiUtils Class
Methods
getRequest(endpoint: ApiEndpoint, token: string): Promise
- Makes a GET request to the specified API endpoint.deleteRequest(endpoint: ApiEndpoint, token: string): Promise
- Makes a DELETE request to the specified API endpoint.postRequest(endpoint: ApiEndpoint, token: string, data: {}): Promise
- Makes a POST request to the specified API endpoint with the provided data.
Explore More
Ready to automate your server?
Join our community, dive into our resources, and start building with the My-MC.Link API and MyMCLib.