A simple tool enabling you to asynchronously upload images to imgbb. Requires Python 3.5+
Install this package using pip
.
$ pip install git+https://github.com/extr3mis/imgbb.git
Import the package using the given import statement.
$ from imgbb.client import Client
Now you can make a Client
object and initialize it with your own API key from imgbb. Optionally, also pass the aiohttp.ClientSession()
object you want the imgbb client to use.
$ myclient = Client(api_key_here)
Now you can use your Client
to upload an image using the Client.post()
method. Since Client.post()
is a coroutine
make sure to await
it. Catch the response from the request in request
.
$ response = await myclient.post('/path_to_image/image.jpg','name')
Now you can get the URL to the image you've just uploaded using response['data']['url']
.
$ URL = response['data']['url']
from imgbb.client import Client
import os
import aiohttp
import asyncio
key = os.getenv('IMGBB_API_KEY')
session = aiohttp.ClientSession()
myclient = Client(key,session)
async def upload(image,name):
response = await myclient.post(image,name)
url = response['data']['url']
print(f'Uploaded image URL: {url}')
if __name__=='__main__':
asyncio.run(upload('image.jpg','Cool picture'))