Next.js Discord

Discord Forum

Api Request not working in production

Answered
Barbary Lion posted this in #help-forum
Open in Discord
Barbary LionOP
My api request to the youtube api is working in localhost, and returns all the video data but the production version doesn't work. If anyone has any input I would appreciate it.
Answered by Yellow-throated Vireo
Its either:
const apiKey = process.env.YT_API_KEY;
const headeddChannel = process.env.YT_HEADEDD_ID;
const headedCSGOChannel = process.env.YT_HEADEDCSGO_ID;
const headedCSChannel = process.env.YT_HEADEDCS_ID;

One of these guys right here in incorrect in your prod envs

or you exceeded your google quota limit
View full answer

88 Replies

American Chinchilla
For starters, is there any error log message you have installed?
it be good to check that out
Yellow-throated Vireo
We need an error message to work with, could be anything
Double check your env for your youtube api and if you didnt use an env you have to fix that ASAP
@Yellow-throated Vireo Double check your env for your youtube api and if you didnt use an env you have to fix that ASAP
Barbary LionOP
I've checked variable names and tried 2 different api keys that both work in local & in thunder client.
@American Chinchilla For starters, is there any error log message you have installed?
Barbary LionOP
No errors from vercel other than a bad request but there is nothing to it
@Barbary Lion No errors from vercel other than a bad request but there is nothing to it
Yellow-throated Vireo
What is the bad request error
Barbary LionOP
It's actually not even coming up bad request anymore
I don't see any requests to the youtube api in network tab
Yellow-throated Vireo
Can you show the request message?
@Barbary Lion I don't see any requests to the youtube api in network tab
Yellow-throated Vireo
Are you using some kind of
Rtk query?
or tanstack query with a {skip: "sadas"}
Barbary LionOP
Nah just a normal fetch with a revalidate every 7200s
const fetchVideo = async (url: string) => {

    try {
        const response = await fetch(url, { next: { revalidate: 7200 } });

        if (!response.ok) {
            console.error(response.statusText);
            return null;
        }

        const data = await response.json();
        // console.log(data)

        return data;
    } catch (error) {
        console.error(error);
        return null;
    }
};
Yellow-throated Vireo
Okay so nothing at all in the console or networks tab?
No errors or 500 bad request?
Barbary LionOP
Not that I can see no
Yellow-throated Vireo
Can you check the actual site with inspect tools and try to check the networks tab there?
Barbary LionOP
Only google analytic requests
Yellow-throated Vireo
Yeah no youtube api requestst
What about your console?
Barbary LionOP
It's weird too because there are no youtube api requests on localhost either but it works fine
Barbary LionOP
In console it just returns the data as expected since I'm printing it out
@Yellow-throated Vireo huh
Barbary LionOP
Exactly why I'm so confused lol
Yellow-throated Vireo
Are you sure there's no youtube api requests?
Barbary LionOP
Positive had another dev friend look through them all multiple times
Yellow-throated Vireo
Is your history on for networks logs?
Barbary LionOP
I don't know what that means tbh
I just refresh the page and the network tab refreshes
Yellow-throated Vireo
1 sec
Try filtering by fetch/xhr
Barbary LionOP
Ah, tried all and fetch
nothing for either
Yellow-throated Vireo
and preserving the log
Preserving the log too?
Barbary LionOP
What's that?
@Yellow-throated Vireo Click to see attachment
Yellow-throated Vireo
The top part
Barbary LionOP
ah yea
that's on
Yellow-throated Vireo
Just so the networks tab doesnt reset on reload
@Barbary Lion Click to see attachment
Yellow-throated Vireo
Could it be because you have "google" on the search
Barbary LionOP
I tried removing that too
Yellow-throated Vireo
This seems rather strange honestly
I think we need a bit more context to solve your issue though
American Chinchilla
can you log the error?
something like try catch
or the api may return an error message
Yellow-throated Vireo
He's already doing that but apparently no errors
Barbary LionOP
Ok so for more context, I'm combining all the 3 channels' videos into one and returning it as one for the client, then I unpack all the json on the client side.https://hastebin.de/owumurahiq.typescript
When I console log the combined videos all the data is as expected
import { fetchCombinedVideos } from "@/fetch-videos";
import VideoOutline from "./video-outline";

type Props = {
    limit: number;
}

type Video = {
    id: {
        kind: string,
        videoId: string,
    }
    snippet: {
        title: string;
        publishedAt: string;
        channelTitle: string;
        thumbnails: {
            default: {
                url: string;
            };
            medium: {
                url: string;
            };
            high: {
                url: string;
            };
        };
    };
};

const VideoPalette = async ({ limit }: Props) => {

    const videos = await fetchCombinedVideos();

    console.log(videos);

    return (
        <div className="w-full h-full flex md:flex-wrap md:flex-row flex-col md:gap-x-12 gap-x-4 md:items-start items-center justify-center md:gap-y-12 gap-y-4">
            {videos.slice(0, limit).map((video: Video, index: number) => (
                <VideoOutline key={index} id={video.id.videoId} thumbnail={video.snippet.thumbnails.high.url} title={video.snippet.title} postDate={video.snippet.publishedAt} channel={video.snippet.channelTitle} />
            ))}
        </div>
    );
}

export default VideoPalette;
Console logging videos here returns data working as intended
And it works on local host
American Chinchilla
so only fetching individually fails?
Barbary LionOP
But when I push to production it just doesn't work. It was working a few days ago then randomly broke
@American Chinchilla so only fetching individually fails?
Barbary LionOP
I have no way to determine that because it's working client side and vercel isn't logging out anything
American Chinchilla
did you define api keys
in server side?
Barbary LionOP
yea
American Chinchilla
did you verify they exist?
in the server side
Barbary LionOP
it was working then just randomly broke which makes me think it was a google api issue, but checked and the token works fine
American Chinchilla
like the env variables
@American Chinchilla did you define api keys
Yellow-throated Vireo
It still doesn't explain why his catch console.logs aren't firing
American Chinchilla
yeah
Yellow-throated Vireo
Is everything else workign properly Renner?
Barbary LionOP
Yes, the leaderboards request to 5 separate apis and they work fine
Yellow-throated Vireo
it seems like your fetchVideo function is just not firing at all if you don't get any logs from it
Barbary LionOP
when I redeployed got this error
Yellow-throated Vireo
hmm
Can you check
your google api quota?
Yellow-throated Vireo
Its either:
const apiKey = process.env.YT_API_KEY;
const headeddChannel = process.env.YT_HEADEDD_ID;
const headedCSGOChannel = process.env.YT_HEADEDCSGO_ID;
const headedCSChannel = process.env.YT_HEADEDCS_ID;

One of these guys right here in incorrect in your prod envs

or you exceeded your google quota limit
Answer
Barbary LionOP
I checked it's not exceeded
maybe because I'm missing the headedcs...
seems like it's working now thanks for the help can't believe it was that simple 😭
Yellow-throated Vireo
Hahaha happens man
American Chinchilla
A good practice is to always throw an error
If the api key is not defined
Or something misconfigured