The following guide shows you how to monetize an OpenAI assistant with Apiable.
What's the difference between an OpenAI Assistant and ChatGPT?
An OpenAI assistant is a programmable version of OpenAI's ChatGPT. You can create a new assistant on the OpenAI platform and give it instructions so that it will behave in much the same way as ChatGPT only via an API.
Set up your OpenAI Platform Account
Navigate to the OpenAI Platform and create and log in or create a new account.
Select "API" when prompted:
Create a new Project:
Creating a new API Assistant
If you do not already have an new OpenAI Assistant then you may create one now.
I already have a Custom GPT can I connect to that instead?
No. OpenAI do not currently support connecting an existing (trained) Custom GPT via the API. You need to re-create it but you may copy paste the instructions into your assistant.
Navigate to assistants.
Create and configure a new assistant. If you already have instructions for your ChatGPT you can copy paste them in here.
Get an API Key
Navigate to API Keys.
Create a new API Secret Key.
Owned by: Service Account
Project: Select the project you just created
Save your Key!
Testing your API Key
We can test that the API is working Using a simple cURL call:
curl "https://api.openai.com/v1/assistants?order=desc&limit=20" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "OpenAI-Beta: assistants=v2"
Replace $OPENAI_API_KEY with the API Key that you created above.
This should provide a response similar to the following:
Connecting your OpenAI Assistant to Amazon API Gateway
Connecting your OpenAI assistant to an API gateway, in this case AWS, will enable you to control access. Specifically you will get these benefits:
Ability to create API keys for each of your API consumers.
Control the quota (usage) of the API for each of your API consumers.
Add a layer of security to protect your assistant from attack.
This tutorial will guide you through the process of setting up the APIable Gateway Role, S3 bucket, and Firehose Stream using AWS CLI and AWS CDK. We’ll also cover how to set up the necessary tools, including Cygwin on Windows and Node.js (npm) using NVM. Let’s get started!
Precondition Setup
⚠️ The precondition setup is optional. If you have a Terminal or Cygwin, Node.js, AWS CLI and CDK installed, you can skip this part.
1. Install Cygwin on Windows
Cygwin allows you to run a Unix-like environment on Windows. Here's how to install it:
Go to the Cygwin website.
Click Download and run the setup file.
During installation, ensure you select the following packages:
curl
git
openssh
Complete the installation and open the Cygwin terminal.
2. Install Node.js and npm using NVM
For Mac/Unix Systems:
Open the Terminal app.
Install Homebrew if not already installed:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Use Homebrew to install NVM:
brew install nvm
Create an NVM directory and update your shell configuration file:
mkdir ~/.nvm echo 'export NVM_DIR="$HOME/.nvm"' >> ~/.zshrc echo '[ -s "/usr/local/opt/nvm/nvm.sh" ] && \. "/usr/local/opt/nvm/nvm.sh"' >> ~/.zshrc source ~/.zshrc
Install Node.js and npm using NVM:
nvm install node
For Windows:
Open Cygwin.
Install NVM by running:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
Update your Cygwin shell config file (e.g.,
~/.bashrc
) and add the following:export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
Reload the shell config file:
source ~/.bashrc
Install Node.js and npm using NVM:
nvm install node
3. Install AWS CLI
For Mac/Unix Systems:
In the terminal, run:
curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg" sudo installer -pkg AWSCLIV2.pkg -target /
Verify the installation:
aws --version
For Windows:
In the Cygwin terminal, run:
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" unzip awscliv2.zip sudo ./aws/install
Verify the installation:
aws --version
4. Install AWS CDK
In the terminal (Mac or Cygwin), run the following to install the AWS CDK:
npm install -g aws-cdk
Verify the installation:
cdk --version
5. Set AWS Key/Secret
Create the
.aws
directory and config files:mkdir -p ~/.aws touch ~/.aws/credentials
Open the
credentials
file in a text editor and add your AWS Access Key and Secret Key:[default] aws_access_key_id = YOUR_AWS_ACCESS_KEY_ID aws_secret_access_key = YOUR_AWS_SECRET_ACCESS_KEY
Save and close the file.
6. Run cdk bootstrap
In the terminal, run:
cdk bootstrap
This prepares your AWS account to use AWS CDK.
7. Clone the APIable CDK Repository
Open the terminal (Mac or Cygwin).
Run the following command to clone the repository:
git clone https://github.com/apiable/cdk cd cdk
8. Deploy the Gateway Role
Run the following command to install the Gateway Role:
./deploy-gatewayrole.sh $AWS_ACCOUNT_ID $AWS_REGION
Example:
./deploy-gatewayrole.sh 034444869755 eu-central-1
Copy & Paste the outputs of the run into some editor, which will look similar to this
Save this RoleARN, you'll need it for the next step!
9. Deploy the S3 Logs Bucket
Run the following command to install the S3 bucket where logs will be collected:
./deploy-apiable-logs-bucket.sh $AWS_ACCOUNT_ID $AWS_REGION $STACKNAME
Example:
./deploy-logs-bucket.sh 034444869755 eu-central-1 dev
Copy & Paste the outputs of the run into some editor, which will look similar to this
10. Deploy the Firehose Stream
Take note of the ARN from the S3 bucket you created in the previous step.
Run the following command to install the Firehose stream:
./deploy-usagelogs.sh $AWS_ACCOUNT_ID $AWS_REGION $LOGS_BUCKET_ARN $STACKNAME
Example:
./deploy-usagelogs.sh 034444869755 eu-central-1 arn:aws:s3:::apiable-logs-dev dev
Copy & Paste the outputs of the run into some editor, which will look similar to this
11. Deploy the GPT Proxy
Run the following command to install the Gateway API connection to ChatGPT:
./deploy-gpt-proxy.sh $AWS_ACCOUNT_ID $AWS_REGION $STACKNAME $API_KEY $ASSISTANT_ID
$API_KEY
: ChatGPT service key$ASSISTANT_ID
: ID of the assistant created in the ChatGPT Dashboard
Example:
./deploy-gpt-proxy.sh 034444869755 eu-west-2 dev $API_KEY $ASSISTANT_ID
Copy & Paste the outputs of the run into some editor, which will look similar to this
11. Enable token usage reporting in the gateway
Go to the API Gateway console and select the deployed Gateway
Select the API
Go to the Stages
Select the Stage
Go to the Logs/Tracing and click Edit
Enable "Custom access logging"
Insert the FirehoseArn from the Output of Step 10 into "Access log destination ARN".
Paste following pattern in the "Log format" field and click save. It MUST be one-line:
{"api_id": "$context.apiId","api_key": "$context.identity.apiKey","key_id": "$context.identity.apiKeyId","ip": "$context.identity.sourceIp","method": "$context.httpMethod","uri": "$context.path","response_size":"$context.responseLength","response_status": "$context.status","resource_id": "$context.resourceId","request_id": "$context.requestId","request_latency": "$context.responseLatency","request_time":"$context.requestTimeEpoch","stage": "$context.stage", "usage_prompt_tokens": "$context.responseOverride.header.usageprompttokens", "usage_completion_tokens": "$context.responseOverride.header.usagecompletiontokens", "usage_total_tokens": "$context.responseOverride.header.usagetotaltokens"}
You're All Set Up!
Now you have successfully set up the Apiable Gateway Role, S3 bucket, Firehose Stream, and Gateway API connection and enabled token usage reporting. You are ready to start using the system with Apiable!
Required information for the next step
The following information is required to complete the next step in this guide:
AWS Account ID
AWS Region
Role ARN
The AWS Account ID and Region may be found at the top right hand corner of your AWS Console:
The Role ARN is given in step 8.
Creating a new API Portal, API Product, and Plans
Create Portal
Sign up to create a new API Portal by choosing a plan at https://www.apiable.io/pricing
Please note: After your payment has been processed, we will build your API Portal. This can take a while, depending on demand. Each Portal is built in its own environment, which allows us to customize it to your requirements in the future.
Apply Theme
Navigate to Portal > Theme
Here you can adjust the look a feel of your API Portal to match your brand.
Connect API Gateway
Navigate to Integrations > API Gateways and click on the + Add Gateway button.
Select Amazon API Gateway - Basic
Fill in the details you collected from the previous step in this guide Connecting your OpenAI Assistant to Amazon API Gateway
Those are:
AWS Account ID
AWS Region
Role ARN
Click on the test button at the bottom of the form. This should show a green "success" when you have completed the steps correctly.
Connect Stripe
Navigate to Integrations > Payment Providers
Click on the Connect with Stripe button.
You will be redirected to Stripe to complete this flow. There are two possibilities:
You already have a Stripe Account. Use the same email address and select your existing account after login.
You need a new Stripe Account. Follow the flow in order to create a new account.
Create a Product
Navigate to Products > API Products and click on the + Add another Product button.
Fill in the product details as required.
Create a Plan
Within your new Product, click on the API Plans tab.
Click on the Create a new plan box.
APIs
Click on the APIs tab.
Select the API Gateway you created in the previous step.
The GPT API you created earlier should now be shown. Select this.
Save the Plan.
Security
Click on the Security tab and select Level 0 from the drop down list.
Documentation
Under the Documentation tab, create a new version for this plan.
Select Manual Upload and use the attached get-proxy-demo-openapi-spec.json
This is linked at the bottom of this page!
Monetization
On the Monetization tab, it's possible to create a new revenue model for your API.
A common pre-paid model is Flat-Fee with overage.
Select the Bill Processing type Token. This will ensure that the token usage is reported to Stripe for each subscriber.Note: If token is unavailable as an option, something may have gone wrong during the setup of the Amazon API Gateway. Please check your configuration.
To complete the tutorial, set the tiers that you want to bill the customer at. A good reference is the OpenAI API Pricing page.
Publish your new Product!
Activate the Plan.
Make sure the Active toggle is green.
Activate the Product.
Make sure the Active toggle is green.
Try out your new Portal
At the top of the dashboard is a link to open your new API Portal.
Have fun!