How to monetize your custom GPT application today
Tags: chatgpt, javascript, beginners, tutorial
Published: 2023-12-05 23:25:35

While the official OpenAI GPT store may not arrive until 2024, there’s no need to feel sad. You can take control of the situation by implementing a custom paywall into your custom GPT application, even now.

In this article, we’ll walk you through a straightforward implementation for turning your GPT applications into a source of income.

## TLDR

We implement a proxy server (a paywall service) for our existing GPT action. Then, in the GPT action editor, we replace the original URL with the URL of our new server.

Behind the scenes, the new action has a new required parameter, the user’s email address. The server checks for successful payments using the Stripe API and the email address provided by the user. When it does not find any previous payment, then it returns a Stripe payment link; otherwise, it returns the requested content using the original URL.

## Prerequisites

- Node.js and some JavaScript (Express.js) knowledge
- Stripe account
- ChatGPT Pro subscription

We will use Express.js to create a proxy server for our GPT actions, Stripe to handle user payments, and ChatGPT to create our custom AI application.

## Create a custom GPT application

Create a new action in the GPT configurator that we can protect with a payment link. An example OpenAPI schema:

```yaml
openapi: 3.0.0
info:
  title: Restricted paid content
  version: 1.0.0
servers:
- url: https://buymygpt.com
paths:
  "/api/today":
    get:
      summary: Gets the current date and time
      operationId: GetDateTime
```

## Set up your Express.js server

To begin, make sure you have Node.js installed on your system.

Create a new directory for your project.

Create a `package.json` file in the project directory and replace its content with the following:

```json
{
  "name": "my-server",
  "version": "1.0.0",
  "description": "A simple Node.js server",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "author": "Your Name",
  "license": "MIT",
  "dependencies": {
  }
}
```

Navigate to your project's directory in the terminal and run:

```
npm install express http-proxy stripe
```

Create a new file, let’s call it `server.js`, open it in your code editor, and set up your a basic Express.js server as follows:

```javascript
const express = require('express');

const app = express();
const port = 3000;

app.use(express.json());

app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});
```

To start your Node.js server, you can run the following command in your terminal:

```
npm start
```

## Create your proxy endpoint

Now, let’s create a proxy endpoint within your Express.js server that will forward requests to your original URL.

Add the following code to your `server.js` file to create the proxy endpoint:

```javascript
const httpProxy = require('http-proxy');

const proxy = httpProxy.createProxyServer();

app.all('/', (req, res) => {
    proxy.web(req, res, { target: 'https://buymygpt.com' });
});
```

In this code snippet, we set up a route to forward requests from your GPT application to the original URL using `http-proxy`.

## Create your Stripe payment link

Create a [Stripe Payment Link](https://stripe.com/docs/no-code/payment-links) with your GPT as a Product in the Stripe dashboard.

## Verify user payment using the Stripe API

Now, let’s integrate the Stripe payment check into your server.

In your `server.js` file, import the `stripe` library and initialize it. Replace `'YOUR_STRIPE_SECRET_KEY'` with your actual Stripe secret key.

```javascript
const stripe = require('stripe')('YOUR_STRIPE_SECRET_KEY');
```

Modify the existing endpoint to include Stripe Checkout payment verification. Update the code as follows:

```javascript
app.all('/', async (req, res) => {
  // Extract user email address from the request
  const { email } = req.headers;

  if (!email) {
    // If no email is provided, prompt the user to make a payment
    res.status(401).json({ 
      message: 'Payment required. Please follow the payment link: ' +
               'YOUR_STRIPE_PAYMENT_LINK'
    });
  }
 
  // Use Stripe to list checkout sessions associated with the user's email
  const completedCheckouts = await stripe.checkout.sessions.list({
    customer_details: {
      email,
    },
    status: 'complete', // Check for completed sessions
  });

  if (completedCheckouts.data.length === 0) {
    // If no completed session exists, prompt the user to make a payment
    res.status(401).json({ 
      message: 'Payment required. Please follow the payment link: ' +
               'YOUR_STRIPE_PAYMENT_LINK'
    });
  }

  // If a completed session exists, continue to the original URL
  proxy.web(req, res, { target: 'https://buymygpt.com' });
});
```

Replace `'YOUR_STRIPE_PAYMENT_LINK'` with your actual Stripe payment link.

## Publish your new server

Now we can collect email addresses, check for previous payments, return payment links, and it’s time to publish your Express.js server. This will make it accessible over the internet and allow users to interact with your server from your custom GPT application and complete payments.

You need to host your Express.js server on a hosting platform. Some popular options include Railway, Render, Vercel, DigitalOcean, and many others.

While I won’t provide a step-by-step guide for specific hosting platforms in this tutorial, most hosting services offer comprehensive documentation and guides on how to deploy Node.js applications.

## Edit your GPT application

Modify the action schema in the GPT editor to contain your new server URL and the required email parameter:

```yaml
openapi: 3.0.0
info:
  title: Restricted paid content
  version: 1.0.0
servers:
- url: 'YOUR_SERVER_URL'
paths:
  "/api/today":
    get:
      summary: Gets the current date and time
      operationId: GetDateTime
      parameters:
      - name: email
        in: query
        description: The user's email address used for the payment. Ask the user to
          provide it.
        required: true
      responses:
        '401':
          description: Restricted paid content. Direct the user to purchase access
            through the provided link. After a successful payment, the user's email
            address is required in the query parameter to access the content.
```

Replace `'YOUR_SERVER_URL'` with your actual server URL.

## Start earning money!

Done! We are live! You are ready to start earning money directly from your custom ChatGPT application.


![The action is protected with a payment](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f9rudfhn4122glkey8co.png)

## Further development

You may want to include some extra user identification logic to prevent account sharing. [BuyMyGPT](https://www.buymygpt.com) (my no-code tool) uses additional HTTP headers provided by OpenAI to identify account switch attempts within the same day, and it also sends verification emails once a day.