Build An Alexa Skill with In-Skill Purchases using ASK Python SDK
Setup the ASK CLI
There are several aspects of developing an Alexa skill with in-skill purchases that require the use of the Alexa Skills Kit Command Line Interface (ASK CLI), so this entire walkthrough will require you to have installed and configured the ASK CLI. If you haven't done this before, here are the resources you need to get the ASK CLI installed on your machine:
If you have used the ASK CLI previously, you will also need to make sure that you have the most recent version of the ASK CLI. You can make sure you have the latest version by running the command:
$ npm update -g ask-cli
Setup the ASK SDK for Python
We have provided the code for this skill on here. To properly upload this code to Lambda, you'll need to perform the following:
This skill uses the ASK SDK for Python for development. The skill code is provided in the lambda_function.py, and the dependencies are mentioned in requirements.txt. Download the contents of the lambda/py folder.
On your system, navigate to the lambda folder and install the dependencies in a new folder called “skill_env” using the following command:
pip install -r py/requirements.txt -t skill_env
Copy the contents of the
lambda/pyfolder into the
cp -r py/* skill_env/
Zip the contents of the
skill_envfolder. Remember to zip the contents of the folder and NOT the folder itself.
On the AWS Lambda console, change the code entry type drop-down to Upload a .ZIP file, upload the zip created in the previous step and click on Save.
(Optional) Follow the ASK Python SDK Getting Started documentation, to check alternative ways of installing the sdk and deploying to AWS Lambda console.
On the Triggers section, add Alexa Skills Kit as a trigger and click on Save.
Create Skill on Developer Console
Create a new skill by following these steps:
- Log in to the Alexa Skills Kit Developer Console.
- Click the Create Skill button in the upper right.
Premium Facts Sampleas your skill name and click Next.
- For the model, select Custom and click Create skill.
Next, define the interaction model for the skill from the JSON Editor. Select JSON Editor from the sidebar and replace the contents with the contents provided in this JSON file, and save it.
Build the skill model.
Navigate to the skill endpoints and add the previously created AWS Lambda Function ARN as the Default Region endpoint. Save the endpoints.
Local installation for ASK CLI and Skill
Create a new directory for CLI work and navigate to the directory.
Premium_Facts_Sampleskill created above using the CLI. This would get you the skill metadata and lambda code cloned at a single place, for working with ISP.
$ ask clone
Navigate to your project folder.
$ cd Premium_Facts_Sample
Explore the project structure. You should see folders for lambda and models, and skill.json file.
$ ls lambda models skill.json
Creating In-Skill Products
There are ASK CLI commands for creating your in-skill purchases. This guide will walk you through creating three different one-time purchases (entitlements), as well as a subscription. Our sample code is expecting these to be created as described, so make sure to follow along carefully.
Create your first in-skill product. You should be in the project's root directory.
$ ask add isp
? List of in-skill product types you can choose (Use arrow keys) ❯ Entitlement Subscription
Choose Entitlement_Template as your template.
? List of in-skill product templates you can choose (Use arrow keys) ❯ Entitlement_Template
Name your in-skill product science_pack.
? Please type in your new in-skill product name: (Entitlement_Template) science_pack
Repeat steps #2 - #5 to create two more entitlements: history_pack and space_pack.
? Please type in your new in-skill product name: (Entitlement_Template) history_pack ... ? Please type in your new in-skill product name: (Entitlement_Template) space_pack
Create a subscription product named all_access using a similar process.
$ ask add isp ? List of in-skill product types you can choose (Use arrow keys) Entitlement ❯ Subscription ? List of in-skill product templates you can choose (Use arrow keys) ❯ Subscription_Template ? Please type in your new in-skill product name: (Subscription_Template) all_access
Navigate to the new ISPs directory, and note the two folders, entitlement and subscription. This is where the JSON files for each of your in-skill products reside.
$ cd isps $ ls
Navigate to the entitlement folder. You should see three files in this directory, one for each of the entitlements you created in our previous steps.
$ cd entitlement $ ls
This JSON file contains all of the necessary fields for your in-skill product, but you'll need to add the details to get them ready to sell. Because we used the Entitlement_Template template, we have provided a small explanation for each field, make sure you replace all of them. Take a look at the sample in our docs for an additional reference. For this sample, at a minimum, you will need to update the name (not referenceName!), smallIconUri, largeIconUri, summary, description, purchasePromptDescription, boughtCardDescription, releaseDate and privacyPolicyUrl. Alternatively you can copy and paste the contents of the files found here: ISP Entitlements.
After updating history.pack.json, Fill out the details for the science_pack.json and space_pack.json files. You will need to update with content about your science and space products including icons for each.
IMPORTANT: Don't change the referenceName in your files, as our codebase is relying on those to be consistent.
Once you are happy with your pricing, descriptions, and the other metadata for your three entitlements, you should update the same fields plus the subscriptionPaymentFrequency for your subscription. Alternatively you can copy and paste the contents of All Access ISP subscription sample into your all_access.json file.
Review and edit the subscription file.
$ cd ../subscription $ open all_access.json
Now that you have customized your in-skill products, you can deploy your skill using the ASK CLI, and start testing it.
Note: Be sure to review the output to confirm there were no errors.
Navigate to the project's root directory. You should see a file named 'skill.json' there.
$ cd ../..
Deploy the skill and the Lambda function in one step by running the following command:
$ ask deploy
Assuming that you followed all of the setup instructions for the ASK CLI, your entire skill and Lambda function should be created on their respective portals.
To test, login to Alexa Developer Console, click on the Premium Facts Sample entry in your skill list, and click on the "Test" tab. The "Test" switch on your skill should have been automatically enabled. If it was not, enable it now.
Your skill can now be tested on devices associated with your developer account, as well as the Test tab in the Developer Portal. To start using your skill, just type or say:
Alexa, open premium facts sample
Note: The developer account associated with the skill is never charged for in-skill products. For more details about testing skills with in-skill products, please refer to the In-Skill Purchase Testing Guide
- Amazon Developer Forums : Join the conversation!
- Hackster.io - See what others are building with Alexa.
Tutorials & Guides
- Voice Design Guide - A great resource for learning conversational and voice user interface design.