REST Class
The class responsible for managing HTTP requests to the Lavalink server. It is instantiated via a Node
instance.
new REST(node: Node)
The REST API is used internally by Moonlink.js. Most users won't need to interact with it directly, as the Manager and Player classes provide higher-level abstractions.
Overview
Properties
Core properties of the REST class.
Available Properties
Property | Type | Description |
---|---|---|
node | Node | The associated Node instance. |
url | string | Base URL for requests to the Lavalink server. |
defaultHeaders | Record<string, string> | Default headers used for all requests. |
Configuration
The REST class is configured through the Node
instance it is associated with. It inherits connection details and authentication from the Node.
You do not directly configure the REST class with host, port, or password. These are derived from the Node
object passed to its constructor.
Methods
loadTracks
Load Tracks
Loads tracks from an identifier. Supports direct URLs, and special prefixes for TTS sources.
Parameters
Returns & Example
Returns
• Promise<IRESTLoadTracks>
// Load a track from YouTube
const result = await rest.loadTracks('youtube', 'Never Gonna Give You Up');
// Load a playlist
const result = await rest.loadTracks('youtube', 'https://youtube.com/playlist?list=...');
// Load a TTS track
const ttsResult = await rest.loadTracks('flowerytts', 'Hello, world!');
update
Update Player
Updates a player's state on the Lavalink server.
Parameters
guildId
and data
for the player update. Returns & Example
Returns
• Promise<unknown>
await rest.update({
guildId: '123456789',
data: {
track: 'base64EncodedTrack',
volume: 100
}
});
destroy
Destroy Player
Removes a player from the Lavalink server.
Parameters
Returns & Example
Returns
• Promise<unknown>
await rest.destroy('123456789');
getInfo
Get Info
Gets information about the Lavalink server.
Returns & Example
Returns
• Promise<unknown>
const info = await rest.getInfo();
getStats
Get Stats
Gets statistics from the Lavalink server.
Returns & Example
Returns
• Promise<unknown>
const stats = await rest.getStats();
getVersion
Get Version
Gets the Lavalink server version.
Returns & Example
Returns
• Promise<unknown>
const version = await rest.getVersion();
getLyrics
Get Lyrics
Gets the lyrics for a track.
Parameters
Returns & Example
Returns
• Promise<IRESTGetLyrics>
const lyrics = await rest.getLyrics({ encoded: 'base64EncodedTrack' });
updateSession
Update Session
Updates a session on the Lavalink server.
Parameters
Returns & Example
Returns
• Promise<any>
await rest.updateSession('session-123', { timeout: 60000 });
decodeTrack
Decode Track
Decodes a base64 encoded track.
Parameters
Returns & Example
Returns
• Promise<any>
const trackInfo = await rest.decodeTrack('base64EncodedTrack');
decodeTracks
Decode Multiple Tracks
Decodes multiple base64 encoded tracks.
Parameters
Returns & Example
Returns
• Promise<any>
const tracksInfo = await rest.decodeTracks(['base64Track1', 'base64Track2']);
getPlayers
Get Players
Gets all players in a session.
Parameters
Returns & Example
Returns
• Promise<IRESTGetPlayers>
const players = await rest.getPlayers('session-123');
getPlayer
Get Player
Gets information about a specific player.
Parameters
Returns & Example
Returns
• Promise<any>
const player = await rest.getPlayer('session-123', '123456789');
getRoutePlannerStatus
Get Route Planner Status
Retrieves the status of the Lavalink Route Planner, which manages IP addresses for outgoing connections.
Returns & Example
Returns
• Promise<any>
— Route Planner status object.
const status = await rest.getRoutePlannerStatus();
console.log(status);
unmarkFailedAddress
Unmark Failed Address
Unmarks a specific IP address as failed in the Route Planner, allowing it to be used again.
Parameters
Returns & Example
Returns
• Promise<any>
await rest.unmarkFailedAddress('192.168.1.1');
unmarkAllFailedAddresses
Unmark All Failed Addresses
Unmarks all currently failed IP addresses in the Route Planner.
Returns & Example
Returns
• Promise<any>
await rest.unmarkAllFailedAddresses();
patch
Patch Request
Sends a generic PATCH request to a specified path on the Lavalink server.
Parameters
sessions/your-session-id
). Returns & Example
Returns
• Promise<unknown>
await rest.patch('sessions/my-session-id', { resuming: true });
get
Generic GET Request
Sends a generic GET request to a specified path on the Lavalink server.
Parameters
Returns & Example
Returns
• Promise<unknown>
const data = await rest.get('sessions/my-session-id/players');
put
Generic PUT Request
Sends a generic PUT request to a specified path on the Lavalink server.
Parameters
Returns & Example
Returns
• Promise<unknown>
await rest.put('sessions/my-session-id/player', { volume: 100 });
post
Generic POST Request
Sends a generic POST request to a specified path on the Lavalink server.
Parameters
Returns & Example
Returns
• Promise<unknown>
await rest.post('sessions/my-session-id/player/play', { track: 'encodedTrack' });
delete
Generic DELETE Request
Sends a generic DELETE request to a specified path on the Lavalink server.
Parameters
Returns & Example
Returns
• Promise<unknown>
await rest.delete('sessions/my-session-id/player');
Usage Example
Basic Usage
Example of how to use the REST class.
Code Example
// Create a REST instance through a node
const rest = node.rest;
// Load a track
const result = await rest.loadTracks('youtube', 'Never Gonna Give You Up');
// Update a player
await rest.update({
guildId: '123456789',
data: {
track: result.tracks[0].track,
volume: 100
}
});
// Get server information
const info = await rest.getInfo();
console.log('Lavalink version:', info.version);
// Decode a track
const trackInfo = await rest.decodeTrack(result.tracks[0].track);
console.log('Track title:', trackInfo.title);