The Random Image API generates random images for all your placeholder and design needs. It supports custom sizes as well as custom image categories.
HTTP GET
Returns a random, base64-encoded image in JPEG format.
category
(optional) - image category. If set, must be one of the following: nature
, city
, technology
, food
, still_life
, abstract
, wildlife
.
width
(optional) - width of the image to generate. Must be between 1
and 5000
. Default value is 640
.
height
(optional) - height of the image to generate. Must be between 1
and 5000
. Default value is 480
.
X-Api-Key
(required) - API Key associated with your account.
Accept
(required) - header indicating the content type to accept in the result. Must be set to the following:image/jpg
Live Demo!
https://api.api-ninjas.com/v1/randomimage?category=
import requests
import shutil
category = 'nature'
api_url = 'https://api.api-ninjas.com/v1/randomimage?category={}'.format(category)
response = requests.get(api_url, headers={'X-Api-Key': 'YOUR_API_KEY', 'Accept': 'image/jpg'}, stream=True)
if response.status_code == requests.codes.ok:
with open('img.jpg', 'wb') as out_file:
shutil.copyfileobj(response.raw, out_file)
else:
print("Error:", response.status_code, response.text)
var data = 'https://api-ninjas.com';
var category = 'nature'
$.ajax({
method: 'GET',
url: 'https://api.api-ninjas.com/v1/randomimage?category=' + category,
headers: { 'X-Api-Key': 'YOUR_API_KEY', 'Accept': 'image/jpg'},
success: function(result) {
console.log(result);
},
error: function ajaxError(jqXHR) {
console.error('Error: ', jqXHR.responseText);
}
});