javascriptmusicbrainz

How to make a browse request to the musicBrainz api with the offset and limit arguments


In the musicbrainz documentation, it is said that:

"Any browse request supports an 'offset=' argument to get more results. Browse requests also support 'limit=': the default limit is 25, and you can increase that up to 100."

So I tried increasing the limit to 100 and fetching the result for each offset like so:

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

async function main() {
    let mbid = "9c9f1380-2516-4fc9-a3e6-f9f61941d090";
    let releases = [];

    let response = await fetch(`https://musicbrainz.org/ws/2/release?artist=${mbid}&inc=recordings+release-groups+ratings&limit=100&fmt=json`);
    let jsonResponse = await response.json();
    releases.push(jsonResponse.releases);
    let releaseCount = jsonResponse["release-count"];

    for (let offset = 100; offset < releaseCount; offset += 100) {
        await sleep(1000);
        let response = await fetch(`https://musicbrainz.org/ws/2/release?artist=${mbid}&inc=recordings+release-groups+ratings&offset=${offset}&limit=100&fmt=json`);
        let jsonResponse = await response.json();
        releases.push(jsonResponse.releases);
    }

    if(releases.flat(1).length == releaseCount){
        console.log("All releases were fetched");
    }else{
        console.log("Problem: some releases were not fetched");
    }

    console.log(releases);
}

main();

The problem is that the releases that I get from the api are not equal to the release-count. This problem doesn't occur if I set the limit to 25 (default) and loop over offsets with an increment of 25 instead of 100.

How can I use paging with a limit of 100 as explained in the musicbrainz api documentation and get all the releases and not just a subset of them?


Solution

  • This appears to be a bug in the MusicBrainz server. I have reported it as such: https://tickets.metabrainz.org/browse/MBS-11892

    If you remove "recordings" from the include list, your code should work. Alternatively, as you have already discovered, using a smaller offset limit appears to work as well.