-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Create API text-to-image example
- Loading branch information
Jeff Shillitto
committed
May 5, 2024
1 parent
652a69f
commit eb29477
Showing
3 changed files
with
121 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
const Shotstack = require('shotstack-sdk'); | ||
|
||
const defaultClient = Shotstack.ApiClient.instance; | ||
const DeveloperKey = defaultClient.authentications['DeveloperKey']; | ||
const api = new Shotstack.CreateApi(); | ||
|
||
const apiUrlBase = 'https://api.shotstack.io/create/'; | ||
let apiUrl = apiUrlBase + 'stage'; | ||
|
||
const id = process.argv[2]; | ||
|
||
if (!id) { | ||
console.log(">> Please provide the UUID of the asset generation task (i.e. node examples/create-api/status.js 01gx3-2827k-dxmpz-x5n32-chw4oq)\n"); | ||
process.exit(1); | ||
} | ||
|
||
if (!process.env.SHOTSTACK_KEY) { | ||
console.log('API Key is required. Set using: export SHOTSTACK_KEY=your_key_here'); | ||
process.exit(1); | ||
} | ||
|
||
if (process.env.SHOTSTACK_HOST) { | ||
apiUrl = process.env.SHOTSTACK_HOST; | ||
} | ||
|
||
if (process.env.SHOTSTACK_ENV) { | ||
apiUrl = apiUrlBase + process.env.SHOTSTACK_ENV; | ||
} | ||
|
||
defaultClient.basePath = apiUrl; | ||
DeveloperKey.apiKey = process.env.SHOTSTACK_KEY; | ||
|
||
api.getGeneratedAsset(id).then((asset) => { | ||
const status = asset.data.attributes.status; | ||
|
||
console.log(`Status: '${status}'\n`); | ||
|
||
if (status == 'done') { | ||
console.log(`>> Asset URL: ${asset.data.attributes.url}`); | ||
} else if (status == 'failed') { | ||
console.log('>> Something went wrong, rendering has terminated and will not continue.'); | ||
} else { | ||
console.log('>> Processing in progress, please try again shortly.\n>> Note: Processing may take some time to complete.'); | ||
} | ||
}, (error) => { | ||
console.error('Request failed or not found: ', error); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const Shotstack = require('shotstack-sdk'); | ||
|
||
const defaultClient = Shotstack.ApiClient.instance; | ||
const DeveloperKey = defaultClient.authentications['DeveloperKey']; | ||
const api = new Shotstack.CreateApi(); | ||
|
||
const apiUrlBase = 'https://api.shotstack.io/create/'; | ||
let apiUrl = apiUrlBase + 'stage'; | ||
|
||
if (!process.env.SHOTSTACK_KEY) { | ||
console.log('API Key is required. Set using: export SHOTSTACK_KEY=your_key_here'); | ||
process.exit(1); | ||
} | ||
|
||
if (process.env.SHOTSTACK_CREATE_HOST) { | ||
apiUrl = process.env.SHOTSTACK_CREATE_HOST; | ||
} | ||
|
||
if (process.env.SHOTSTACK_ENV) { | ||
apiUrl = apiUrlBase + process.env.SHOTSTACK_ENV; | ||
} | ||
|
||
defaultClient.basePath = apiUrl; | ||
DeveloperKey.apiKey = process.env.SHOTSTACK_KEY; | ||
|
||
const textToImage = new Shotstack.ShotstackTextToImageOptions; | ||
textToImage.setWidth(1024).setHeight(1024).setPrompt('A realistic photo of the planet Mars with a black outer space background'); | ||
|
||
const generatedAsset = new Shotstack.ShotstackGeneratedAsset; | ||
generatedAsset.setOptions(textToImage); | ||
|
||
api.postGenerateAsset(generatedAsset).then((asset) => { | ||
const status = asset.data.attributes.status; | ||
const id = asset.data.id | ||
|
||
console.log(`Request '${status}' with id: ${id}\n`); | ||
console.log('>> Now check the progress of image by running:'); | ||
console.log(`>> node examples/create-api/status.js ${id}`); | ||
}, (error) => { | ||
console.error('Request failed: ', error); | ||
process.exit(1); | ||
}); |