Uploading image to Imgur with Bunjs

imgur, bunjs, code, image-upload

  • No need external dependency except bun-types

export async function uploadToImgur(imageData: ArrayBuffer): Promise<string> {
	const response = await fetch("https://api.imgur.com/3/image", {
		method: "POST",
		body: imageData,
		headers: {
			Authorization: "Client-ID dd32dd3c6aaa9a0",
			"Content-Type": "application/octet-stream"
		}
	})
	if (!response.ok) throw new Error(`Failed to upload image: ${response.statusText}`)

	const responseData = await response.json()
	return responseData.data.link
}

export async function uploadFromPath(imagePath: string): Promise<string> {
	const imageFile = Bun.file(imagePath)
	const imageBytes = await imageFile.arrayBuffer()
	return await uploadToImgur(imageBytes)
}

Last updated

© 2024 ~ Yunus Emre Ak ~ yEmreAk