Getting a status code 404 when fetching private files #56
Answered
by
stevedylandev
Akinbola247
asked this question in
Q&A
-
i'm trying to read private files from pinata, but it doesn't work. Tried using javascript, i got a 404 error. i used curl too, getting the same error :
CURL
JAVASCRIPT export async function POST(req: Request) {
try {
// Get raw text
const rawBody = await req.text();
console.log('Raw body:', rawBody);
// Clean up unexpected trailing characters like %
const cleanedBody = rawBody.trim().replace(/%$/, '');
let parsed;
try {
parsed = JSON.parse(cleanedBody);
} catch (jsonError) {
throw new Error('Invalid JSON body');
}
const { cid } = parsed;
console.log('Parsed CID:', cid);
if (!cid) {
return NextResponse.json(
{ error: 'CID parameter is required' },
{ status: 400 }
);
}
// Ensure JWT is present
const PINATA_JWT = process.env.PINATA_JWT;
if (!PINATA_JWT) {
throw new Error('Missing PINATA_JWT environment variable');
}
// Use the correct Pinata endpoint
const pinataEndpoint = `https://api.pinata.cloud/v3/files/download_link`;
const payload = JSON.stringify({
url: `https://example.mypinata.cloud/files/${cid}`,
expires: 3600, // 1 hour expiration
date: 1724875300,
method: "GET"
});
const response = await fetch(pinataEndpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.PINATA_JWT}`
},
body: payload
});
console.log('Response status:', response);
if (!response.ok) {
const errorText = await response.text();
console.error('Pinata API error:', errorText);
throw new Error(`Pinata API error: ${response.statusText}`);
}
const responseData = await response.json();
if (!responseData.downloadLink) {
throw new Error('Missing downloadLink in response');
}
return NextResponse.json({
url: responseData.downloadLink,
expiresAt: responseData.expiresAt || Date.now() + 3600000,
});
} catch (error) {
console.error('Error:', error);
return NextResponse.json(
{
error: 'Failed to generate access link',
details: error instanceof Error ? error.message : 'Unknown error',
},
{ status: 500 }
);
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
stevedylandev
Apr 19, 2025
Replies: 1 comment 9 replies
-
Hey there! Could you give us more info about the file you uploaded? Perhaps use this endpoint and give us the response? |
Beta Was this translation helpful? Give feedback.
9 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I took a closer look at your original code and hte curl request and I think you might need to tweak a few things to make a valid request. In the body of the of the request you're sending to Pinata you're not passing in your own gateway url, and instead it's the example. We also want to dynamically pass in the date vs hard coding it in. At the very end of the code you're sending something that doesn't exist on the response, it should just be
responseData.data
per the docs. Can you try using this code instead and make sure you haveNEXT_PUBLIC_GATEWAY_URL
set with your own gateway in the format ofmy-example-gateway.mypinata.cloud
?