Amazon Personalize examples using SDK for JavaScript V3 - Amazon SDK for JavaScript
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China.

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

Starting October 1, 2022, Amazon SDK for JavaScript (v3) will end support for Internet Explorer 11 (IE 11). For details, see Announcing the end of support for Internet Explorer 11 in the Amazon SDK for JavaScript (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 Amazon SDK for JavaScript V3 with Amazon Personalize.

Actions are code excerpts that show you how to call individual Amazon Personalize functions.

Scenarios are code examples that show you how to accomplish a specific task by calling multiple Amazon Personalize functions.

Each example includes a link to GitHub, 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 create a Amazon Personalize batch interface job.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

// 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: { /* required */ s3DataSource: { /* required */ path: 'INPUT_PATH', /* required */ // kmsKeyArn: 'INPUT_KMS_KEY_ARN' /* optional */' } }, jobOutput: { /* required */ s3DataDestination: { /* required */ path: 'OUTPUT_PATH', /* required */ // kmsKeyArn: 'OUTPUT_KMS_KEY_ARN' /* optional */' } }, roleArn: 'ROLE_ARN', /* required */ solutionVersionArn: 'SOLUTION_VERSION_ARN', /* required */ numResults: 20 /* optional integer*/ }; 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 create a Amazon Personalize batch segment job.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

// 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: { /* required */ s3DataSource: { /* required */ path: 'INPUT_PATH', /* required */ // kmsKeyArn: 'INPUT_KMS_KEY_ARN' /* optional */' } }, jobOutput: { /* required */ s3DataDestination: { /* required */ path: 'OUTPUT_PATH', /* required */ // kmsKeyArn: 'OUTPUT_KMS_KEY_ARN' /* optional */' } }, roleArn: 'ROLE_ARN', /* required */ solutionVersionArn: 'SOLUTION_VERSION_ARN', /* required */ numResults: 20 /* optional */ }; 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 create a Amazon Personalize campaign.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

// 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 Amazon SDK for JavaScript API Reference.

The following code example shows how to create a Amazon Personalize dataset.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

// 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 Amazon SDK for JavaScript API Reference.

The following code example shows how to create a Amazon Personalize dataset export job.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

// 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 create a Amazon Personalize dataset group.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

// 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 create a Amazon Personalize dataset import job.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

// 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 create a Amazon Personalize domain schema.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

// 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 'fs'; let 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 Amazon SDK for JavaScript API Reference.

The following code example shows how to create a Amazon Personalize filter.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

// 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 Amazon SDK for JavaScript API Reference.

The following code example shows how to create a Amazon Personalize recommender.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

// 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 create a Amazon Personalize schema.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

// 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 'fs'; let 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();
  • For API details, see CreateSchema in Amazon SDK for JavaScript API Reference.

The following code example shows how to create a Amazon Personalize solution.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

// 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 Amazon SDK for JavaScript API Reference.

The following code example shows how to create a Amazon Personalize solution.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

// 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();

The following code example shows how to create a Amazon Personalize event tracker.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

// 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 import real-time interaction event data into Amazon Personalize Events.

SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

// Get service clients module and commands using ES6 syntax. import { PutItemsCommand } from "@aws-sdk/client-personalize-events"; import { personalizeEventsClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeEventsClient = new PersonalizeEventsClient({ region: "REGION"}); // Set the put items parameters. For string properties and values, use the \ character to escape quotes. var putItemsParam = { datasetArn: 'DATASET_ARN', /* required */ items: [ /* required */ { 'itemId': 'ITEM_ID', /* required */ 'properties': "{\"PROPERTY1_NAME\": \"PROPERTY1_VALUE\", \"PROPERTY2_NAME\": \"PROPERTY2_VALUE\", \"PROPERTY3_NAME\": \"PROPERTY3_VALUE\"}" /* optional */ } ] }; export const run = async () => { try { const response = await personalizeEventsClient.send(new PutItemsCommand(putItemsParam)); console.log("Success!", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();
  • For API details, see PutItems in Amazon SDK for JavaScript API Reference.