If you're building a website, blog, or app, image hosting can become a hassleโespecially when you're looking for a free and reliable option. Thankfully, imgbb provides a developer-friendly API that allows you to upload images and retrieve direct URLs. In this post, you'll learn how to use Python to upload images to imgbb and get the hosted image link.
imgbb.com is a free image hosting platform that allows you to upload and store images and get shareable URLs. It supports an easy-to-use REST API, which is perfect for scripting with Python.
- Go to imgbb API page
- Click on Get API Key
- Sign in or sign up
- Once signed in, you'll receive your API key
Note: Keep this key secret. Donโt share it in public code repositories.
We'll use Pythonโs built-in requests
library. If you don't have it, install it using:
pip install requests
Hereโs a simple Python script to upload an image and get its hosted URL:
๐ Copy
import requests
API_KEY = 'your_imgbb_api_key' # Replace this with your actual key
IMAGE_PATH = 'your_image.jpg' # Local path to the image you want to upload
with open(IMAGE_PATH, 'rb') as file:
url = "https://api.imgbb.com/1/upload"
payload = {
"key": API_KEY,
"image": file.read()
}
response = requests.post(url, files=payload)
if response.status_code == 200:
data = response.json()
print("โ
Image uploaded successfully!")
print("๐ Image URL:", data['data']['url'])
else:
print("โ Upload failed:", response.text)
โ
Image uploaded successfully!
๐ Image URL: https://i.ibb.co/xyz123/my-uploaded-image.jpg
- You can upload from a URL too, using
source
instead ofimage
. - imgbb has upload limits (like size and rate limits). For large-scale use, consider a premium plan.
- Make sure your API key isn't exposed in public GitHub repos.
With just a few lines of Python, you can automate image hosting using imgbbโfree and easy. This can be useful for developers building blogs, documentation, or any content-rich apps that require image hosting without dealing with full-fledged storage solutions.
Happy coding!