AWS Security ChangesHomeSearch

AWS code-library documentation change

Service: code-library · 2025-03-02 · Documentation low

File: code-library/latest/ug/bedrock-runtime_example_bedrock-runtime_InvokeModel_AmazonNovaImageGeneration_section.md

Summary

Added JavaScript code example for using Amazon Nova Canvas image generation with Bedrock Runtime, including setup, request configuration, and response handling

Security assessment

The change adds a code example demonstrating image generation functionality. There is no mention of security vulnerabilities, authentication, or security-related features in the added content.

Diff

diff --git a/code-library/latest/ug/bedrock-runtime_example_bedrock-runtime_InvokeModel_AmazonNovaImageGeneration_section.md
index d620fc808..8e211c3a0 100644
--- a/code-library/latest/ug/bedrock-runtime_example_bedrock-runtime_InvokeModel_AmazonNovaImageGeneration_section.md
+++ b/code-library/latest/ug/bedrock-runtime_example_bedrock-runtime_InvokeModel_AmazonNovaImageGeneration_section.md
@@ -215,0 +216,108 @@ Create an image with Amazon Nova Canvas.
+JavaScript
+    
+
+**SDK for JavaScript (v3)**
+    
+
+###### Note
+
+There's more on GitHub. Find the complete example and learn how to set up and run in the [AWS Code Examples Repository](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javascriptv3/example_code/bedrock-runtime#code-examples). 
+
+Create an image with Amazon Nova Canvas.
+    
+    
+    import {
+      BedrockRuntimeClient,
+      InvokeModelCommand,
+    } from "@aws-sdk/client-bedrock-runtime";
+    import { saveImage } from "../../utils/image-creation.js";
+    import { fileURLToPath } from "node:url";
+    
+    /**
+     * This example demonstrates how to use Amazon Nova Canvas to generate images.
+     * It shows how to:
+     * - Set up the Amazon Bedrock runtime client
+     * - Configure the image generation parameters
+     * - Send a request to generate an image
+     * - Process the response and handle the generated image
+     *
+     * @returns {Promise<string>} Base64-encoded image data
+     */
+    export const invokeModel = async () => {
+      // Step 1: Create the Amazon Bedrock runtime client
+      // Credentials will be automatically loaded from the environment
+      const client = new BedrockRuntimeClient({ region: "us-east-1" });
+    
+      // Step 2: Specify which model to use
+      // For the latest available models, see:
+      // https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html
+      const modelId = "amazon.nova-canvas-v1:0";
+    
+      // Step 3: Configure the request payload
+      // First, set the main parameters:
+      // - prompt: Text description of the image to generate
+      // - seed: Random number for reproducible generation (0 to 858,993,459)
+      const prompt = "A stylized picture of a cute old steampunk robot";
+      const seed = Math.floor(Math.random() * 858993460);
+    
+      // Then, create the payload using the following structure:
+      // - taskType: TEXT_IMAGE (specifies text-to-image generation)
+      // - textToImageParams: Contains the text prompt
+      // - imageGenerationConfig: Contains optional generation settings (seed, quality, etc.)
+      // For a list of available request parameters, see:
+      // https://docs.aws.amazon.com/nova/latest/userguide/image-gen-req-resp-structure.html
+      const payload = {
+        taskType: "TEXT_IMAGE",
+        textToImageParams: {
+          text: prompt,
+        },
+        imageGenerationConfig: {
+          seed,
+          quality: "standard",
+        },
+      };
+    
+      // Step 4: Send and process the request
+      // - Embed the payload in a request object
+      // - Send the request to the model
+      // - Extract and return the generated image data from the response
+      try {
+        const request = {
+          modelId,
+          body: JSON.stringify(payload),
+        };
+        const response = await client.send(new InvokeModelCommand(request));
+    
+        const decodedResponseBody = new TextDecoder().decode(response.body);
+        // The response includes an array of base64-encoded PNG images
+        /** @type {{images: string[]}} */
+        const responseBody = JSON.parse(decodedResponseBody);
+        return responseBody.images[0]; // Base64-encoded image data
+      } catch (error) {
+        console.error(`ERROR: Can't invoke '${modelId}'. Reason: ${error.message}`);
+        throw error;
+      }
+    };
+    
+    // If run directly, execute the example and save the generated image
+    if (process.argv[1] === fileURLToPath(import.meta.url)) {
+      console.log("Generating image. This may take a few seconds...");
+      invokeModel()
+        .then(async (imageData) => {
+          const imagePath = await saveImage(imageData, "nova-canvas");
+          // Example path: javascriptv3/example_code/bedrock-runtime/output/nova-canvas/image-01.png
+          console.log(`Image saved to: ${imagePath}`);
+        })
+        .catch((error) => {
+          console.error("Execution failed:", error);
+          process.exitCode = 1;
+        });
+    }
+    
+    
+
+  * For API details, see [InvokeModel](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/bedrock-runtime/command/InvokeModelCommand) in _AWS SDK for JavaScript API Reference_. 
+
+
+
+