UNPKG

persistent-image-url

Version:

A lightweight Node.js library for persisting image URLs using the [imgbb.com](https://imgbb.com/) or [sm.ms](https://sm.ms/) API. This library allows you to upload images from temporary URLs and get back persistent URLs that can be used to access the imag

30 lines (24 loc) 718 B
import axios from "axios"; const SMMS_URL = "https://sm.ms/api/v2/upload"; export async function uploadImageToSmms( imageBase64: string, token: string ): Promise<string> { try { // convert base64 to blob const blob = await fetch(` data:application/octet-stream;base64,${imageBase64} `).then((r) => r.blob()); const formData = new FormData(); formData.append("smfile", blob); const res = await axios.post(SMMS_URL, formData, { headers: { "Content-Type": "multipart/form-data", Authorization: token, }, }); return res.data.data.url; } catch (error) { throw new Error("Unable to upload image to Smms"); } }