This is my API route:
And the code:
import type { NextApiRequest, NextApiResponse } from "next";
import axios from "axios";
import prisma from "../../../lib/prisma";
interface Platform {
id: number;
abbreviation: string;
alternative_name: string;
category: number;
created_at: number;
generation: number;
name: string;
platform_logo: {
id: number;
alpha_channel: boolean;
animated: boolean;
height: number;
image_id: string;
url: string;
width: number;
checksum: string;
};
slug: string;
summary: string;
updated_at: number;
url: string;
versions: number[];
checksum: string;
}
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const url = "https://api.igdb.com/v4/platforms";
const headers = {
"client-id": process.env.TWITCH_IGDB_CLIENT_ID,
authorization: "Bearer my_token",
"accept-encoding": "*",
};
const body = `fields *, platform_family.*, platform_logo.*;
limit 500;`;
const response = await axios.post(url, body, { headers });
console.log("The response status is:", response.status);
if (response.status == 200) {
await response.data.forEach(async (p: Platform) => {
try {
const platform = await prisma.platform
.create({
data: {
category: p.category,
name: p.name,
slug: p.slug,
platform_logo_url: p.platform_logo?.url, // Todo, sanitize this url.
hashtag: p.name, // Todo, convert name to hashtag
},
});
console.log("Created platforms", platform);
} catch (e) {
console.log(e)
}
});
} else {
console.log(response.status);
}
res.status(200).json({ message: "Imported all platforms." });
}
Locally I hit the url: http://localhost:3000/api/igdb/refresh_platforms and I can see the 199 platforms in my local database.
On Vercel, I sometimes get 3 inserted, sometimes none inserted, sometimes, 55 inserted.
It seems completely arbitrary and I'm totally confused.
What am I doing wrong here?