How to use uploadthing server side?

Is this the correct way?
export const utapi = new UTApi(); // imported from uploadthing/server

// Extract image URLs and upload them to UploadThing
const uploadPromises = validatedData.map(async (item) => {
if (item.image) {
const response = await utapi.uploadFilesFromUrl(item.image);
if (response.error) {
throw new Error("Error uploading image");
}
item.image = response.data.url; // Replace with UploadThing URL
}
return item;
});
export const utapi = new UTApi(); // imported from uploadthing/server

// Extract image URLs and upload them to UploadThing
const uploadPromises = validatedData.map(async (item) => {
if (item.image) {
const response = await utapi.uploadFilesFromUrl(item.image);
if (response.error) {
throw new Error("Error uploading image");
}
item.image = response.data.url; // Replace with UploadThing URL
}
return item;
});
7 Replies
Aleed.dev
Aleed.dev5mo ago
are you using next ? @MarvinKR
import { UTApi } from "uploadthing/server";
export const utapi = new UTApi();

// in ur upload file
import { utapi } from "~/server/uploadthing.ts";

async function uploadFiles(formData: FormData) {

const files =formData.getAll("files");
const response = await utapi.uploadFiles(files);
// ^? UploadedFileResponse[]
}
import { UTApi } from "uploadthing/server";
export const utapi = new UTApi();

// in ur upload file
import { utapi } from "~/server/uploadthing.ts";

async function uploadFiles(formData: FormData) {

const files =formData.getAll("files");
const response = await utapi.uploadFiles(files);
// ^? UploadedFileResponse[]
}
Aleed.dev
Aleed.dev5mo ago
this should work for you
MarvinKR
MarvinKROP5mo ago
Not sure if I understand how it translates in Hono
c2
c25mo ago
just read the docs more bro 🤷🏿‍♂️ look at hono routing and what uploadthing is doing also just test
MarvinKR
MarvinKROP5mo ago
Did that, but it’s not clear. Examples are client side
c2
c25mo ago
so you are using uploadthing in combination with ssr? @MarvinKR
Want results from more Discord servers?
Add your server