0

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.

3
  • check connection from your next server to service api: curl -v https://cloud-save.services.api.unity.com Commented Sep 12 at 14:17
  • If you think it shouldn't take this long, then the server or network has an issue. Otherwise, increase the time out. Which case applies to you? Commented Sep 12 at 14:53
  • 10 seconds was a decent wait, so it does sound like something else is wrong networking wise Commented Sep 12 at 17:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.