I’m building a Next.js route that fetches leaderboard data and then queries the Cloud Save API for each player’s public Email item.
The issue. TypeError: fetch failed [cause]: [Error [ConnectTimeoutError]: Connect Timeout Error (attempted address: cloud-save.services.api.unity.com:443, timeout: 10000ms)] { code: 'UND_ERR_CONNECT_TIMEOUT' }
Here's the code
export async function GET() {
const bearerToken = await getBearerToken();
const res = await fetch(
`https://leaderboards.services.api.unity.com/v1/projects/${UNITY_PROJECT_ID_FRED}/leaderboards/luxembourg/scores?limit=1000`,
{
headers: {
Authorization: `Bearer ${bearerToken}`,
'Content-Type': 'application/json',
},
}
);
if (!res.ok) {
return NextResponse.json(
{ error: 'Failed to fetch leaderboard' },
{ status: res.status }
);
}
const leaderboard: LeaderboardResponse = await res.json();
const playerIds = leaderboard.results?.map((r) => r.playerId) ?? [];
const emails = await Promise.all(
playerIds.map(async (playerId: string) => {
const emailRes = await fetch(
`https://cloud-save.services.api.unity.com/v1/data/projects/${UNITY_PROJECT_ID_FRED}/players/${playerId}/public/items?keys=Email`,
{
headers: {
Authorization: `Bearer ${bearerToken}`,
'Content-Type': 'application/json',
},
}
);
if (!emailRes.ok) {
return { playerId, email: null };
}
const data = await emailRes.json();
const emailItem = data.results?.find((r) => r.key === 'Email');
return { playerId, email: emailItem?.value ?? null };
})
);
emails.forEach((e) => console.log(e.email));
return NextResponse.json({ emails });
}
Thanks
On the client side, I have a button that downloads a CSV, but it doesn’t work because of a timeout.
curl -v https://cloud-save.services.api.unity.com