Package Exports
- host-my-pic
- host-my-pic/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (host-my-pic) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
host-my-pic
A simple, zero-config package to quickly upload an image and get a permanent, direct link.
This package is a lightweight wrapper for the ImgBB API. The API key is already included, so you can use it instantly without any setup.
Features
- Zero-Config: No API key or setup required.
- Custom Keys:: Supports providing your own ImgBB API key.(Optional)
- Flexible Input: Accepts both public image URLs and Base64 encoded strings.
- Simple API: Exports a single asynchronous function.
- Direct Links: Returns a direct URL to the image (e.g.,
https://i.ibb.co/.../image.png).
Installation
npm install host-my-picUsage & Examples
The package exports a single default function. It takes one required argument: your image as a public URL or a Base64 string and one optional argument (your API key).
import hostMyPic from "host-my-pic";Example 1: Uploading from a URL
(async () => {
try {
const imageUrl =
"https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png";
const link = await hostMyPic(imageUrl);
console.log("Your new image link is:", link);
} catch (error) {
console.error("Error uploading image:", error.message);
}
})();Example 2: Uploading from a Base64 String
(async () => {
try {
// This can be a long Base64 string from a file input, canvas, etc.
const myBase64String = "iVBORw0KGgoAAAANSUhEUg...";
const link = await hostMyPic(myBase64String);
console.log("Your new image link is:", link);
} catch (error) {
console.error("Error uploading image:", error.message);
}
})();Error Handling
The function will throw an Error if the upload fails. It's highly recommended to wrap your call in a try...catch block.
try {
const link = await hostMyPic(someImage);
console.log("Success:", link);
} catch (err) {
// Handle the error gracefully
console.error("Upload failed:", err.message);
}License
ISC