Skip to content

jfbloom22/custom-gpt-api-oauth

Repository files navigation

Custom GPT Actions API with OAuth2 and Postgres

A guide and cloneable template for creating an OpenAI ChatGPT Custom GPT with user authentication and database access.

Tech Stack

  • Database: Prisma with Neon Serverless Postgres
  • Authentication: Clerk.com OAuth 2.0
  • Hosting: Vercel
  • Language: TypeScript
  • Package Manager: pnpm

Prerequisites

  • Node.js 18+
  • pnpm
  • A custom domain name (required for Clerk configuration)
  • OpenAI API access
  • Clerk account
  • Neon database account
  • Vercel account (optional, for deployment)

Quick Start

  1. Clone the repository
  2. Install dependencies:
    pnpm install
  3. Set up environment variables:
    cp .env.example .env
  4. Update the .env file with your credentials
  5. Initialize the database:
    npx prisma generate
    npx prisma db push

Detailed Setup Guide

1. Create a Custom GPT

  1. Visit the OpenAI GPT Builder
  2. Follow the setup wizard to create your GPT
  3. Configure the Actions section with the OpenAPI specification

2. Configure Clerk Authentication

  1. Create a new Clerk project
  2. Set up a production instance with a custom domain
  3. Create an OAuth2 Provider:
    curl -X POST https://api.clerk.com/v1/oauth_applications \
     -H "Authorization: Bearer <CLERK_SECRET_KEY>"  \
     -H "Content-Type: application/json" \
     -d '{
       "callback_url": "https://oauth-client.com/oauth2/callback",
       "name": "oauth_app_1",
       "scopes": "profile email"
     }'
  4. Update the callback URL after receiving the OpenAPI schema:
    curl -X PATCH https://api.clerk.com/v1/oauth_applications/<oauth_application_id> \
     -H "Authorization: Bearer <CLERK_SECRET_KEY>"  \
     -H "Content-Type: application/json" \
     -d '{"callback_url":"https://oauth-client.com/oauth2/callback"}'
  5. Verify the configuration:
    curl -X GET https://api.clerk.com/v1/oauth_applications \
     -H "Authorization: Bearer <CLERK_SECRET_KEY>"

3. Database Setup

  1. Create a new Neon database
  2. Update the DATABASE_URL in your .env file
  3. Run database migrations:
    npx prisma generate
    npx prisma db push

4. Generate OpenAPI Specifications

Use the ActionsGPT to generate OpenAPI specs based on your Prisma schema and code.

5. Deployment

Option 1: Vercel (Recommended)

  • The project is configured for Vercel deployment
  • Use the Vercel CLI for deployment
  • All /api routes are automatically converted to serverless functions

Option 2: Conventional Node.js Server

  1. Add build script to package.json:
    {
      "scripts": {
        "build": "tsc"
      }
    }
  2. Build the project:
    pnpm build
  3. Use the dist folder as your server root

Development

Database Management

  • Generate Prisma client: npx prisma generate
  • Push schema changes: npx prisma db push
  • Format schema: npx prisma format
  • Create migration: prisma migrate dev --name {name}
  • Deploy migrations: prisma migrate deploy
  • Launch Prisma Studio: npx prisma studio

Local Development

  • Run development server: pnpm dev
  • Note: vercel dev may have issues with app.listen() - use pnpm dev instead

Production Considerations

Before deploying to production, consider implementing:

  • Comprehensive test suite
  • Robust error handling
  • Additional API endpoints
  • Improved API organization following best practices:
    src/
    ├── controllers/
    ├── routes/
    └── api/
    
  • Automated OpenAPI spec generation
  • Consider using TSOA

Privacy Policy

Before publishing your GPT, create a privacy policy. Use this template prompt:

Help me create a simple privacy policy to publish on my website for my custom GPT. 
My company name is [company name] and my website is [website url]. 
The company will collect email addresses, but it will never sell this information to third parties.

Publishing Your GPT

  1. Complete your Builder Profile
  2. Verify your domain name
  3. Submit for review

For more information, visit: Building and Publishing a GPT

Troubleshooting

  • Vercel dev issues: Use pnpm dev instead
  • Prisma type build errors on Vercel: These are ignorable
  • Database connection issues: Verify your DATABASE_URL in .env

Contributing

Contributions are welcome! Please feel free to submit a PR or create an issue.

License

This project is licensed under the MIT License - see the LICENSE.txt file for details.

About

Example Custom GPT with user authentication and the ability to read and write to a database.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published