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.dev4mo 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.dev4mo ago
this should work for you
MarvinKR
MarvinKR4mo ago
Not sure if I understand how it translates in Hono
c2
c24mo ago
just read the docs more bro 🤷🏿‍♂️ look at hono routing and what uploadthing is doing also just test
MarvinKR
MarvinKR4mo ago
Did that, but it’s not clear. Examples are client side
c2
c24mo ago
so you are using uploadthing in combination with ssr? @MarvinKR
Want results from more Discord servers?
Add your server