Skip to content

Setting up images programatically

Boxchat supports image generation using the API.

Environment variables and responses

Environment variables

NameValues
BOXCHAT_COMMERICAL_URLhttps://api-stg.boxchat.ai
BOXCHAT_API_KEYAPI key created by the team

Responses

StatusValues
200{‘image_urls’: [‘IMAGE_URL_HERE’], ‘message’: ‘Images created successfully’}, image was generated successfully.
402Insufficient credits, out of credits.
401Unauthorized, API key is not valid.
400{‘error’: ‘Number of images cannot be greater than 4’}

Examples

Python example

from os import getenv
from dotenv import load_dotenv
import requests
load_dotenv()
boxchat_base_url = getenv("BOXCHAT_COMMERICAL_URL")
boxchat_url = f"{boxchat_base_url}/images/generate"
api_key = getenv("BOXCHAT_API_KEY")
num_of_images = 1
prompt = "husky dog"
data = {
"num_of_images": num_of_images,
"prompt": prompt,
}
response = requests.post(boxchat_url, json=data, headers={"Authorization": f"Bearer {api_key}"})
print(response.json())

Ruby Example

require 'dotenv/load'
require 'net/http'
require 'json'
boxchat_base_url = ENV['BOXCHAT_COMMERICAL_URL']
boxchat_url = "#{boxchat_base_url}/images/generate"
api_key = ENV['BOXCHAT_API_KEY']
num_of_images = 1
prompt = 'husky dog'
uri = URI(boxchat_url)
request = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
request['Authorization'] = "Bearer #{api_key}"
request.body = { num_of_images: num_of_images, prompt: prompt }.to_json
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
http.request(request)
end
puts response.body

Curl Example

Terminal window
curl -X POST "$BOXCHAT_COMMERICAL_URL/images/generate" \
-H "Authorization: Bearer $BOXCHAT_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"num_of_images": 1,
"prompt": "husky dog"
}'

JavaScript example

require('dotenv').config();
const fetch = require('node-fetch');
const boxchatBaseUrl = process.env.BOXCHAT_COMMERICAL_URL;
const boxchatUrl = `${boxchatBaseUrl}/images/generate`;
const apiKey = process.env.BOXCHAT_API_KEY;
const numOfImages = 1;
const prompt = 'husky dog';
const data = {
num_of_images: numOfImages,
prompt: prompt,
};
fetch(boxchatUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`,
},
body: JSON.stringify(data),
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));