[BUG]: getting error on using utapi.deleteFiles
hey, im getting "Missing UPLOADTHING_SECRET env variable." when trying to delete file.
this is my code :
export const deleteFiles = async (fileKey: string) => {
await utapi.deleteFiles(fileKey);
};
i checked the env variables and everything is fine, the upload works
Hey, @alifares10
Where you using upload through UT components, hooks or UT API?
im using uploadFiles function from generateReactHelpers
@alifares10 Are you calling utapi.deleteFiles on client or server?
server
@alifares10 Where do you call it? API endpoint, server action?
Are you using create-t3-app starter or something else?
im calling it in a server component, i moved it to a client one and still got the same problem
im using create next app
Were you able to fix it?
Mine worked by putting "use server" above and adding this to the next.config.js:
experimental: {
serverActions: true,
},
im calling it in a server component, i moved it to a client one and still got the same problem
It definitely won't work in a client component, since it relies on environment variables that don't exist on client. We intended it to be used either in a /api route, or in TRPC for example. It should also work in server actions, but given the current state of those, I don't know that I would necessarily recommend them 😅
Please reopen with a reproduction repo if this issue persists.
Hi, I'm confused about the location of the utapi.deleteFiles. Where should I put it? is in api/uploadthing folder?
You can use the utapi functions anywhere that runs serverside, so yes, /api is a reasonable place to use it.
so in /api/uploadthing I named deleteFile.ts, that I use the code as @alifares10
import { utapi } from "uploadthing/server";
export const deleteFiles = async (fileKey: string) => {
await utapi.deleteFiles(fileKey);
};
When I called deleteFile(fileKey) from client, I got the same error "Missing UPLOADTHING_SECRET env variable."
When I called deleteFile(fileKey) from client, I got the same error "Missing UPLOADTHING_SECRET env variable."
Well, yes. Cause then you're not calling the utapi serverside. You'll jave to make it an API route and call it using fetch in that case.
Ugh. I thought Next.js's best feature was that you don't have to make fetches to internal APIs!
But that aside, what's the best practice on how to secure this? Because I shouldn't make a fetch request with my private API key from a client component, right?
Ugh. I thought Next.js's best feature was that you don't have to make fetches to internal APIs!
But that aside, what's the best practice on how to secure this? Because I shouldn't make a fetch request with my private API key from a client component, right?
How do you make mutations elsewhere in your app?
I'm still a bit of a scrappy developer, so I hope I use the correct terminology.
I created "use server"; functions, like updateUser(), and then I call those from functions inside my components.
But UploadThing doesn't work that same way.
Okay so you're using server actions. You can call utapi.deleteFiles() from that
@juliusmarminge, I thought I was, but it was complaining that utapi.deleteFiles() was not a function.
Did you create an instance of the UTApi?
import { UTApi } from "uploadthing/server";
const utapi = new UTApi(opts);
...
const deleteFile = async (key: string) => {
"use server"
// authenticate user
// verify they have permission to delete
await utapi.deleteFiles(key);
}