Skip to content

Commit

Permalink
Add Create API text-to-image example
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Shillitto committed May 5, 2024
1 parent 652a69f commit eb29477
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 8 deletions.
39 changes: 31 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
- **probe.js** -
Fetch metadata for any media asset on the internet such as width, height, duration, etc...

### Asset management examples
### Storage and hosting examples

- **serve-api/renderId.js** -
Fetch all assets associated with a render ID. Includes video or image and thumbnail and poster.
Expand All @@ -96,6 +96,11 @@
Sends a rendered video to an AWS S3 bucket and excludes it from Shotstack. Requires an AWS account and correctly
configured bucket (see: https://community.shotstack.io/t/s3-permission-issue/397).

### Generative AI examples

- **create-api/text-to-image.js** -
Generate an image using the Shotstack text-to-image provider.

### Installation

Install the required dependencies including the [Shotstack Node SDK](https://www.npmjs.com/package/shotstack-sdk)
Expand Down Expand Up @@ -126,35 +131,53 @@ Shotstack web site.
The examples directory includes a number of examples demonstrating the capabilities of the
Shotstack API.

#### Rendering
#### Video editing (Edit API)

To run a rendering/editing example run the examples at the root of the examples folder, e.g. to run the images video
example:
To run a rendering/editing example run the examples at the root of the examples folder.

To run the images video example:

```bash
node examples/images.js
```

#### Polling

To check the status of a render, similar to polling run the `status.js` example with the render ID, e.g.:
To check the status of a render, run the `status.js` example with the render ID:

```bash
node examples/status.js 8b844085-779c-4c3a-b52f-d79deca2a960
```

#### Asset management
#### Storing and hosting assets (Serve API)

To look up assets hosted by Shotstack run the examples in the [examples/serve-api](./examples/serve-api/) directory.

Find assets by render ID:

```bash
node examples/serve-api/renderId.js 8b844085-779c-4c3a-b52f-d79deca2a960
```

or

Find an asset by asset ID:

```bash
node examples/serve-api/assetId.js 3f446298-779c-8c8c-f253-900c1627b776
```

#### Generating assets using AI (Create API)

To create assets using Generative AI providers run the examples in the [examples/create-api](./examples/create-api/)
directory.

To generate an image using the Shotstack text-to-image provider:

```bash
node examples/create-api/text-to-image.js
```

To check the status of a create task, run the `status.js` example with the asset ID:

```bash
node examples/create-api/status.js 01gx3-2827k-dxmpz-x5n32-chw4oq
```
48 changes: 48 additions & 0 deletions examples/create-api/status.js
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);
});
42 changes: 42 additions & 0 deletions examples/create-api/text-to-image.js
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);
});

0 comments on commit eb29477

Please sign in to comment.