Imgur is a popular online image sharing website. It is one of the most popular services where you can post, share, store and view pictures and GIFs. But it also has an extensive API that can be used with the help of RapidAPI.
How To Get Access to the Imgur API (Imgur API Key)?
1. Sign up for a RapidAPI User Account
To begin using the Imgur API, you’ll first need to sign up for a free RapidAPI developer account.
RapidAPI is the world’s largest API marketplace and offers more than 10,000 APIs and over 1,000,000 developers. Our goal is to help developers find and connect to APIs to help them build amazing apps.
2. Navigate to the Imgur API Page
Search for “Imgur” in the search bar, or click here to go there directly.
3. Subscribe to the Imgur API
Once you have successfully navigated to the Imgur page, click on the “pricing” tab and pick a pricing plan.
4. Register an Imgur application
The next two steps are used to obtain the OAuth 2.0 authorization header which is necessary for all the requests. First, you need to register your Imgur application:
5. Connect the application to your account
Replace the “CLIENT_ID” with your client_id and use the following link to obtain the access token:
https://api.imgur.com/oauth2/authorize?client_id=CLIENT_ID&response_type=token
You will see a screen similar to this where you need to accept the request from your application.
6. Obtain the access token
The response URL will look like this:
https://imgur.com/#access_token=ACCESS_TOKEN&expires_in=315360000&token_type=bearer&refresh_token=REFRESH_TOKEN&account_username=USERNAME&account_id=ID
The field we are interested in is “access_token”. Save it because we will use it in the RapidAPI Imgur demo.
7. Head to the Imgur RapidAPI page and fill in the Header Parameters section
Now, with the Imgur Access Token, you can fill necessary Authorization information on the RapidAPI Imgur page.
– Select your RapidAPI project or use the default one.
– X-RapidAPI-Key should be filled automatically.
– Paste your Access Token into Authorization field in the format
“Bearer ACESS_TOKEN”. For example:
“Bearer 3e17d6a442efeaek4e5tbo5k4e3n7054b630b23d”
Imgur API Endpoints
Imgur API has a lot of endpoints so you can get lost easily. To prevent that, we’ve summarized the description of each endpoint section so you can find what you need easily.
1. Gallery
The Gallery section provides access to various collections of pictures, galleries, and albums, including:
- Subreddit galleries;
- Meme galleries;
- User albums;
- Random galleries.
It also contains endpoints to share, remove, report, vote, comment on albums/images. It also allows us to add/vote on gallery tags.
2. Image
The Image section allows a user to perform basic operations with images like obtaining info about the image, uploading, deleting, updating info about and selecting an image as a favorite.
3. Notification
The Notification section is used to view notifications for a user who is currently logged in.
4. Account
The Account section allows currently logged in users to get information about their accounts and edit their accounts. This information includes:
- Basic information about an account;
- Favorites;
- Submissions(Images);
- Settings;
- Albums;
- Comments;
- Images;
- Replies.
5. Comment
Similar to the Image section, the Comment section allows you to perform operations with comments and replies:
Get, create, delete, and vote for a comment.
6. Album
The Album section is used to perform manipulating operations with albums and images in them.
7. Custom Gallery
The Custom Gallery section allows you to view custom gallery images and also allows you to add and remove tags.
8. Conversation
The Conversation section allows you to get a list of current conversations and to get information about a specific conversation.
9. Memegen
The Memegen section, as the name might imply, is where you’ll gain access to a list of default memes.
How to Download Images from Imgur via API
Now, let’s demonstrate an example of how Imgur API can be used.
We will write a python script that pulls all recent images that have the “cat” tag. Among other uses, such sets of images can be used to train deep learning models. Of course, it’s not guaranteed that every picture with a “cat” tag is actually a picture of a cat, so it’s better to review the dataset before training, or remember that some images in the dataset will be inaccurate.
The endpoint we need is located in the “Gallery” section and is called “Gallery tag”. After filling out Header information we need to specify the tag, “t_string” parameter. In our case, it will be “cat”.
Finally, in the right section, we can choose the code snippet for a request to be represented in the needed language. We will choose Python with the “requests” module. Then your screen should look something like this:
Now we can use the code in the script. But before we do that, let’s check the format of results by pressing the “Test endpoint” button:
As you can see, the information we need to download images is in the “items” array under the “link” field.
Now we are ready for the script code. Here are a few notes about implementation:
- For downloading images we can use ‘urlretrieve’ from ‘urllib.request’.
- Most links in the ‘items’ array are albums, so we need to iterate them separately and check for non-images(GIFS).
- If we wanted to get more images, we could iterate over pages with the ‘page’ parameter of the request.
- All images are saved in ‘jpg’ format, so ‘png’ images will lose in quality, but nothing too bad and we have all images in the same format.
- This is a simple working example that you can easily reproduce:
import requests from urllib.request import urlretrieve url = "https://imgur-apiv3.p.rapidapi.com/3/gallery/t/cat/%7Bsort%7D/%7Bwindow%7D/%7Bpage%7D" headers = { 'x-rapidapi-host': "imgur-apiv3.p.rapidapi.com", 'x-rapidapi-key': "[your rapidapi key]", 'authorization': "Bearer 248dd2f77c8fea5c834c37126623aa2ec908e891" } response = requests.request("GET", url, headers=headers).json() i = 0 for item in response['data']['items']: if(item['is_album'] == True): print('album') for image in item['images']: if('.jpg' in image['link']) or ('.png' in image['link']): urlretrieve(image['link'], f'image_{i}.jpg') i += 1 else: if('.jpg' in item['link']) or ('.png' in item['link']): urlretrieve(item['link'], f'image_{i}.jpg') i += 1
Here are the results of mostly cat images:
Conclusion
In this tutorial, we reviewed Imgur API, its features and how it can be used with RapidAPI. We also made a python script that downloads images by a specific tag from Imgur.
Saad says
Is this token expire if i authorize my app ? or its forever ?