Integrationsbeispiele

MCAPI.TR-Beispiele für Websites, Bots, Serverplugins und die Kommandozeile.

HTML / JavaScript

Voraussetzungen und Verwendung

Moderner Browser. Datei über einen lokalen HTTP-Server öffnen.

Offizielle Dokumentation ↗
index.html
<!doctype html>
<html lang="en"><meta charset="utf-8"><title>Minecraft status</title>
<button id="check">Check server</button><pre id="result" role="status"></pre>
<script>
const address = 'mc.hypixel.net';
document.querySelector('#check').addEventListener('click', async () => {
  const button = document.querySelector('#check');
  const output = document.querySelector('#result');
  button.disabled = true;
  try {
    const response = await fetch('https://mcapi.tr/api/v1/status/' + encodeURIComponent(address), {
      signal: AbortSignal.timeout(20000)
    });
    if (response.status === 404) { output.textContent = 'Offline or unreachable'; return; }
    if (!response.ok) throw new Error('HTTP ' + response.status);
    const data = await response.json();
    output.textContent = (data.stale ? 'Last known data: ' : '') +
      (data.online ? 'Online: ' + (data.players?.online ?? '?') + ' players' : 'Offline');
  } catch (error) { output.textContent = 'Check failed: ' + error.message; }
  finally { button.disabled = false; }
});
</script></html>

Die Beispiele behandeln 404, HTTP-Fehler und stale-Daten. Ergebnisse mindestens 60 Sekunden zwischenspeichern; Bot-Tokens auf dem Server behalten.