Edit in GitHubLog an issue

Quickstart Guide

Generate your first image with Firefly Services

an illustration of a cat coding on a laptop

Prerequisites

Credentials

If you don't already have a Firefly API or Firefly Services Client ID and Client Secret, retrieve them from your Adobe Developer Console project before reading further. Securely store these credentials and never expose them in client-side or public code.

Set Up Your Environment

Before we begin this tutorial, run the following in a secure terminal:

Copied to your clipboard
mkdir firefly-api-generate-images-tutorial
cd firefly-api-generate-images-tutorial
npm init --y
npm install axios qs
touch index.js

Depending on your learning style, you may prefer to walk through this tutorial step-by-step or jump immediately to the full source code.

Retrieve an Access Token

Open a secure terminal and export your Client ID and Client Secret as environment variables so that your later commands can access them:

Copied to your clipboard
export FIREFLY_SERVICES_CLIENT_ID=yourClientIdAsdf123
export FIREFLY_SERVICES_CLIENT_SECRET=yourClientSecretAsdf123

Generate an access token:

Copied to your clipboard
curl --location 'https://ims-na1.adobelogin.com/ims/token/v3' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=client_credentials' \
--data-urlencode "client_id=$FIREFLY_SERVICES_CLIENT_ID" \
--data-urlencode "client_secret=$FIREFLY_SERVICES_CLIENT_SECRET" \
--data-urlencode 'scope=openid,AdobeID,session,additional_info,read_organizations,firefly_api,ff_apis'

The response will look like this:

Copied to your clipboard
{"access_token":"yourAccessTokenAsdf123","token_type":"bearer","expires_in":86399}

Export this access token so that the next script can conveniently access it:

Copied to your clipboard
export FIREFLY_SERVICES_ACCESS_TOKEN=yourAccessTokenAsdf123

Generate an Image

Next, call the Firefly Generate Images API:

Copied to your clipboard
curl --location 'https://firefly-api.adobe.io/v3/images/generate' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header "x-api-key: $FIREFLY_SERVICES_CLIENT_ID" \
--header "Authorization: Bearer $FIREFLY_SERVICES_ACCESS_TOKEN" \
--data '{
"prompt": "a realistic illustration of a cat coding"
}'

The response will look like this:

Copied to your clipboard
{
"size": {
"width": 2048,
"height": 2048
},
"outputs": [
{
"seed": 1779323515,
"image": {
"url": "https://pre-signed-firefly-prod.s3-accelerate.amazonaws.com/images/asdf-12345?lots=of&query=params..."
}
}
],
"contentClass": "art"
}

View the Generated Image

Open the URL in your browser to see the image you generated with Firefly 🎉

Full Example

You can review the prerequisites section to understand how to set up your environment prior to running this code. Note that this is an example only and is not production-ready and requires additional error handling, logging, security measures, and more before you can run it at scale in a live application.

Copied to your clipboard
import os
import requests
def main():
access_token = retrieve_access_token()
generate_image(access_token)
def retrieve_access_token():
client_id = os.environ['FIREFLY_SERVICES_CLIENT_ID']
client_secret = os.environ['FIREFLY_SERVICES_CLIENT_SECRET']
token_url = 'https://ims-na1.adobelogin.com/ims/token/v3'
payload = {
'grant_type': 'client_credentials',
'client_id': client_id,
'client_secret': client_secret,
'scope': 'openid,AdobeID,session,additional_info,read_organizations,firefly_api,ff_apis'
}
response = requests.post(token_url, data=payload)
response.raise_for_status()
token_data = response.json()
print("Access Token Retrieved")
return token_data['access_token']
def generate_image(access_token):
client_id = os.environ['FIREFLY_SERVICES_CLIENT_ID']
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'x-api-key': client_id,
'Authorization': f'Bearer {access_token}'
}
data = {
'prompt': 'a realistic illustration of a cat coding', # Replace with your actual prompt
}
response = requests.post(
'https://firefly-api.adobe.io/v3/images/generate',
headers=headers,
json=data
)
response.raise_for_status()
job_response = response.json()
print("Generate Image Response:", job_response)
# Access the generated image URL
image_url = job_response['outputs'][0]['image']['url']
print(f"You can view the generated image at: {image_url}")
if __name__ == '__main__':
main()

Deepen Your Understanding

Visit the Firefly Generate Image API tutorial to learn more about the rich customization options available to you 🚀

  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.