Next.js Discord

Discord Forum

API returns 404

Answered
Cape lion posted this in #help-forum
Open in Discord
Cape lionOP
I got the following API Route inside of app/api/log/[...uuid].ts. If I visit the route, it just shows a 404. also if I use it in my codes, I get a 404 on this route. what could be the problem?

import { NextApiRequest, NextApiResponse } from 'next';

export default async function GET(
    request: Request,
    response: NextApiResponse,
    { params }: { params: { uuid: string }}
) {
    const uuid = params.uuid;

    try {
        const apiEndpoint = `${process.env.REST_API_URL}/logs`;
        const response = await fetch(apiEndpoint, {
            headers: {
                'Content-Type': 'application/json',
                'API-Key': process.env.REST_API_KEY!,
                'server': uuid,
            },
        });

        if (!response.ok) {
            throw new Error('Failed to fetch log data');
        }

        const data = await response.json();
        return data;
    } catch (error) {
        console.error('Error fetching log data:', error);
        return response.status(500).json({error: 'Internal Server Error'});
    }
}
Answered by Sun bear
api/log/[uuid]/route.ts

Then access it like:

export async function GET(
  request: Request,
  { params }: { params: { uuid: string } }
) {

const { uuid } = params

//..

}
View full answer

7 Replies

Sun bear
The file name of a route always has to be route.ts (in case you use typescript)
@Sun bear The file name of a route always has to be `route.ts` (in case you use typescript)
Cape lionOP
and with that it would also work with api/log/{uuid}?
Sun bear
api/log/[uuid]/route.ts

Then access it like:

export async function GET(
  request: Request,
  { params }: { params: { uuid: string } }
) {

const { uuid } = params

//..

}
Answer
@Sun bear api/log/[uuid]/route.ts Then access it like: js export async function GET( request: Request, { params }: { params: { uuid: string } } ) { const { uuid } = params //.. }
Cape lionOP
thats my route now:

import { NextApiRequest, NextApiResponse } from 'next';

export async function GET(
    request: Request,
    response: NextApiResponse,
    { params }: { params: { uuid: string }}
) {
    const uuid = params.uuid;

    try {
        const apiEndpoint = `${process.env.REST_API_URL}/logs`;
        const response = await fetch(apiEndpoint, {
            headers: {
                'Content-Type': 'application/json',
                'API-Key': process.env.REST_API_KEY!,
                'server': uuid,
            },
        });

        if (!response.ok) {
            throw new Error('Failed to fetch log data');
        }

        const data = await response.json();
        return data;
    } catch (error) {
        console.error('Error fetching log data:', error);
        return response.status(500).json({error: 'Internal Server Error'});
    }
}


I'll get Cannot destructure property 'params' of 'undefined' as it is undefined.

I'm using /api/log/123
Cape lionOP
got it :PeepoHappy:
@Cape lion got it <:PeepoHappy:1107716624410234920>
Sun bear
Did you get it working?
Cape lionOP
yessir, thanks!