BlueprintB
Blueprint3mo ago
2 replies
MinecraftLover

How do I get the server ID from within my custom server route?

I have a server route, /server/{id}/players, and I need to get the {id} part of the server so I can get the IP and port. I also need it so I can send API requests so I can run commands likes kick, ban, and op.
Solution
SOLUTION

Getting Server ID
import { ServerContext } from "@/state/server";

const getServerID = () => {
    const serverContext = ServerContext.useStoreState(state => state.server.data);
    return serverContext?.id;
};


Sending Server Commands
import http from "@/api/http"

const sendCommand = async (command: string) => {
    try {
        await http.post(`/api/client/servers/${serverId}/command`, { command });
    } catch (err) {
        console.error("Error sending command:", err);
        alert("Failed to execute command");
    }
};
Was this page helpful?