HTTP 503 & other CORS errors trying to get some results

I am trying to get a bunch of covers from the MB API but I keep getting 503 errors and other CORS things:

Does anybody know what my issue is? This is what I’m doing (hacky but it’s the best I can do with last.fm…)

async function getArt(
    trackMbid: string, 
    title: string, 
    artistMbid: string
): Promise<string | null> {
    let res = await fetch(`https://musicbrainz.org/ws/2/recording/${trackMbid}?inc=releases&fmt=json`).catch(() => null);
    if (!res || res.status === 503) return null;

    if (!res.ok) {
        let searchData = await (await fetch(`https://musicbrainz.org/ws/2/recording?query=recording:"${encodeURIComponent(title)}" AND arid:${artistMbid}&fmt=json`)).json();
        trackMbid = searchData.recordings?.[0]?.id;
        if (!trackMbid) return null;
        res = await fetch(`https://musicbrainz.org/ws/2/recording/${trackMbid}?inc=releases&fmt=json`).catch(() => null);
    }

    if (!res) return null;

    let response: Response = res;
    let data = await response.json();

    return data.releases?.[0]?.id ? `https://coverartarchive.org/release/${data.releases[0].id}/front` : null;
}

Thanks

You can install an extension that allows/ ignores all cors for localhost if it is just for you.
As alternative you could make it so your server makes the request instead of your browser.

I am going to be running this from the browser, there is no backend, it’s my personal portfolio site.

404s are likely because you request something not existing (check the mbid source, does it match an actual recording?).
503s are mainly because you didn’t respect MusicBrainz API / Rate Limiting - MusicBrainz

5 Likes

1 req/s is really low considering I am not planning on proxying this. Anything I can do?

I can usually get about 1-2 a second consistently, and more if just in short bursts. Try implementing some code to re-try calls and slow down every time one fails. Just don’t do this too often…

Actually you can do bursts of 5 req per 5 seconds. Altough I’m not able to recomend a rate limit package

1 Like