Amazon Personalize examples using SDK for JavaScript (v3) - AWS SDK for JavaScript

The AWS SDK for JavaScript V3 API Reference Guide describes in detail all the API operations for the AWS SDK for JavaScript version 3 (V3).

Amazon Personalize examples using SDK for JavaScript (v3)

The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for JavaScript (v3) with Amazon Personalize.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.

Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.

Topics

Actions

The following code example shows how to use CreateBatchInferenceJob.

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.

// Get service clients module and commands using ES6 syntax. import { CreateBatchInferenceJobCommand } from "@aws-sdk/client-personalize"; import { personalizeClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeClient = new PersonalizeClient({ region: "REGION"}); // Set the batch inference job's parameters. export const createBatchInferenceJobParam = { jobName: "JOB_NAME", jobInput: { s3DataSource: { path: "INPUT_PATH", }, }, jobOutput: { s3DataDestination: { path: "OUTPUT_PATH", }, }, roleArn: "ROLE_ARN", solutionVersionArn: "SOLUTION_VERSION_ARN", numResults: 20, }; export const run = async () => { try { const response = await personalizeClient.send( new CreateBatchInferenceJobCommand(createBatchInferenceJobParam), ); console.log("Success", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();

The following code example shows how to use CreateBatchSegmentJob.

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.

// Get service clients module and commands using ES6 syntax. import { CreateBatchSegmentJobCommand } from "@aws-sdk/client-personalize"; import { personalizeClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeClient = new PersonalizeClient({ region: "REGION"}); // Set the batch segment job's parameters. export const createBatchSegmentJobParam = { jobName: "NAME", jobInput: { s3DataSource: { path: "INPUT_PATH", }, }, jobOutput: { s3DataDestination: { path: "OUTPUT_PATH", }, }, roleArn: "ROLE_ARN", solutionVersionArn: "SOLUTION_VERSION_ARN", numResults: 20, }; export const run = async () => { try { const response = await personalizeClient.send( new CreateBatchSegmentJobCommand(createBatchSegmentJobParam), ); console.log("Success", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();

The following code example shows how to use CreateCampaign.

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.

// Get service clients module and commands using ES6 syntax. import { CreateCampaignCommand } from "@aws-sdk/client-personalize"; import { personalizeClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeClient = new PersonalizeClient({ region: "REGION"}); // Set the campaign's parameters. export const createCampaignParam = { solutionVersionArn: "SOLUTION_VERSION_ARN" /* required */, name: "NAME" /* required */, minProvisionedTPS: 1 /* optional integer */, }; export const run = async () => { try { const response = await personalizeClient.send( new CreateCampaignCommand(createCampaignParam), ); console.log("Success", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();
  • For API details, see CreateCampaign in AWS SDK for JavaScript API Reference.

The following code example shows how to use CreateDataset.

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.

// Get service clients module and commands using ES6 syntax. import { CreateDatasetCommand } from "@aws-sdk/client-personalize"; import { personalizeClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeClient = new PersonalizeClient({ region: "REGION"}); // Set the dataset's parameters. export const createDatasetParam = { datasetGroupArn: "DATASET_GROUP_ARN" /* required */, datasetType: "DATASET_TYPE" /* required */, name: "NAME" /* required */, schemaArn: "SCHEMA_ARN" /* required */, }; export const run = async () => { try { const response = await personalizeClient.send( new CreateDatasetCommand(createDatasetParam), ); console.log("Success", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();
  • For API details, see CreateDataset in AWS SDK for JavaScript API Reference.

The following code example shows how to use CreateDatasetExportJob.

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.

// Get service clients module and commands using ES6 syntax. import { CreateDatasetExportJobCommand } from "@aws-sdk/client-personalize"; import { personalizeClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeClient = new PersonalizeClient({ region: "REGION"}); // Set the export job parameters. export const datasetExportJobParam = { datasetArn: "DATASET_ARN" /* required */, jobOutput: { s3DataDestination: { path: "S3_DESTINATION_PATH" /* required */, //kmsKeyArn: 'ARN' /* include if your bucket uses AWS KMS for encryption }, }, jobName: "NAME" /* required */, roleArn: "ROLE_ARN" /* required */, }; export const run = async () => { try { const response = await personalizeClient.send( new CreateDatasetExportJobCommand(datasetExportJobParam), ); console.log("Success", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();

The following code example shows how to use CreateDatasetGroup.

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.

// Get service clients module and commands using ES6 syntax. import { CreateDatasetGroupCommand } from "@aws-sdk/client-personalize"; import { personalizeClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeClient = new PersonalizeClient({ region: "REGION"}); // Set the dataset group parameters. export const createDatasetGroupParam = { name: "NAME" /* required */, }; export const run = async (createDatasetGroupParam) => { try { const response = await personalizeClient.send( new CreateDatasetGroupCommand(createDatasetGroupParam), ); console.log("Success", response); return "Run successfully"; // For unit tests. } catch (err) { console.log("Error", err); } }; run(createDatasetGroupParam);

Create a domain dataset group.

// Get service clients module and commands using ES6 syntax. import { CreateDatasetGroupCommand } from "@aws-sdk/client-personalize"; import { personalizeClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeClient = new PersonalizeClient({ region: "REGION"}); // Set the domain dataset group parameters. export const domainDatasetGroupParams = { name: "NAME" /* required */, domain: "DOMAIN" /* required for a domain dsg, specify ECOMMERCE or VIDEO_ON_DEMAND */, }; export const run = async () => { try { const response = await personalizeClient.send( new CreateDatasetGroupCommand(domainDatasetGroupParams), ); console.log("Success", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();

The following code example shows how to use CreateDatasetImportJob.

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.

// Get service clients module and commands using ES6 syntax. import { CreateDatasetImportJobCommand } from "@aws-sdk/client-personalize"; import { personalizeClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeClient = new PersonalizeClient({ region: "REGION"}); // Set the dataset import job parameters. export const datasetImportJobParam = { datasetArn: "DATASET_ARN" /* required */, dataSource: { /* required */ dataLocation: "S3_PATH", }, jobName: "NAME" /* required */, roleArn: "ROLE_ARN" /* required */, }; export const run = async () => { try { const response = await personalizeClient.send( new CreateDatasetImportJobCommand(datasetImportJobParam), ); console.log("Success", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();

The following code example shows how to use CreateEventTracker.

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.

// Get service clients module and commands using ES6 syntax. import { CreateEventTrackerCommand } from "@aws-sdk/client-personalize"; import { personalizeClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeClient = new PersonalizeClient({ region: "REGION"}); // Set the event tracker's parameters. export const createEventTrackerParam = { datasetGroupArn: "DATASET_GROUP_ARN" /* required */, name: "NAME" /* required */, }; export const run = async () => { try { const response = await personalizeClient.send( new CreateEventTrackerCommand(createEventTrackerParam), ); console.log("Success", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();

The following code example shows how to use CreateFilter.

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.

// Get service clients module and commands using ES6 syntax. import { CreateFilterCommand } from "@aws-sdk/client-personalize"; import { personalizeClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeClient = new PersonalizeClient({ region: "REGION"}); // Set the filter's parameters. export const createFilterParam = { datasetGroupArn: "DATASET_GROUP_ARN" /* required */, name: "NAME" /* required */, filterExpression: "FILTER_EXPRESSION" /*required */, }; export const run = async () => { try { const response = await personalizeClient.send( new CreateFilterCommand(createFilterParam), ); console.log("Success", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();
  • For API details, see CreateFilter in AWS SDK for JavaScript API Reference.

The following code example shows how to use CreateRecommender.

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.

// Get service clients module and commands using ES6 syntax. import { CreateRecommenderCommand } from "@aws-sdk/client-personalize"; import { personalizeClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeClient = new PersonalizeClient({ region: "REGION"}); // Set the recommender's parameters. export const createRecommenderParam = { name: "NAME" /* required */, recipeArn: "RECIPE_ARN" /* required */, datasetGroupArn: "DATASET_GROUP_ARN" /* required */, }; export const run = async () => { try { const response = await personalizeClient.send( new CreateRecommenderCommand(createRecommenderParam), ); console.log("Success", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();

The following code example shows how to use CreateSchema.

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.

// Get service clients module and commands using ES6 syntax. import { CreateSchemaCommand } from "@aws-sdk/client-personalize"; import { personalizeClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeClient = new PersonalizeClient({ region: "REGION"}); import fs from "node:fs"; const schemaFilePath = "SCHEMA_PATH"; let mySchema = ""; try { mySchema = fs.readFileSync(schemaFilePath).toString(); } catch (err) { mySchema = "TEST"; // For unit tests. } // Set the schema parameters. export const createSchemaParam = { name: "NAME" /* required */, schema: mySchema /* required */, }; export const run = async () => { try { const response = await personalizeClient.send( new CreateSchemaCommand(createSchemaParam), ); console.log("Success", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();

Create a schema with a domain.

// Get service clients module and commands using ES6 syntax. import { CreateSchemaCommand } from "@aws-sdk/client-personalize"; import { personalizeClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeClient = new PersonalizeClient({ region: "REGION"}); import fs from "node:fs"; const schemaFilePath = "SCHEMA_PATH"; let mySchema = ""; try { mySchema = fs.readFileSync(schemaFilePath).toString(); } catch (err) { mySchema = "TEST"; // for unit tests. } // Set the domain schema parameters. export const createDomainSchemaParam = { name: "NAME" /* required */, schema: mySchema /* required */, domain: "DOMAIN" /* required for a domain dataset group, specify ECOMMERCE or VIDEO_ON_DEMAND */, }; export const run = async () => { try { const response = await personalizeClient.send( new CreateSchemaCommand(createDomainSchemaParam), ); console.log("Success", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();
  • For API details, see CreateSchema in AWS SDK for JavaScript API Reference.

The following code example shows how to use CreateSolution.

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.

// Get service clients module and commands using ES6 syntax. import { CreateSolutionCommand } from "@aws-sdk/client-personalize"; import { personalizeClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeClient = new PersonalizeClient({ region: "REGION"}); // Set the solution parameters. export const createSolutionParam = { datasetGroupArn: "DATASET_GROUP_ARN" /* required */, recipeArn: "RECIPE_ARN" /* required */, name: "NAME" /* required */, }; export const run = async () => { try { const response = await personalizeClient.send( new CreateSolutionCommand(createSolutionParam), ); console.log("Success", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();
  • For API details, see CreateSolution in AWS SDK for JavaScript API Reference.

The following code example shows how to use CreateSolutionVersion.

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.

// Get service clients module and commands using ES6 syntax. import { CreateSolutionVersionCommand } from "@aws-sdk/client-personalize"; import { personalizeClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeClient = new PersonalizeClient({ region: "REGION"}); // Set the solution version parameters. export const solutionVersionParam = { solutionArn: "SOLUTION_ARN" /* required */, }; export const run = async () => { try { const response = await personalizeClient.send( new CreateSolutionVersionCommand(solutionVersionParam), ); console.log("Success", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();