Build a GPT That Talks to Your Database in One Day
Tags: oauth2, openai, chatgpt, node
Published: 2024-06-27 01:52:13

Have you ever wondered how challenging it is to create a Custom GPT with user authentication and database access?   I found the lack of examples of this disheartening. So, I create a comprehensive guide myself and am pleased to say, with a small amount of coding skills you can build your own in day.

* [GitHub Repository: Custom GPT API OAuth](https://github.com/jfbloom22/custom-gpt-api-oauth)
* [Demo: My Pizza Dough \(OAuth Demo\)](https://chatgpt.com/g/g-oXRAoqOK8-my-pizza-dough-oauth-demo)

### Tech Stack
To achieve this, I used:
* Clerk.com: for authentication
* [Vercel](https://vercel.com): for hosting
* [Prisma](https://www.prisma.io/): for great database management UX
* [Neon](https://neon.tech/): for serverless postgres
* Typescript
* Express.js

### Who is this for?
This guide is perfect for developers looking to create an AI Agent or a Custom GPT that supports user authentication and database access with ease.

### Why it matters
An AI Agent with user authentication unlocks numerous possibilities, enabling applications that require secure user data access and personalized experiences.

### Prerequisites
Before diving in you will need:
* Familiarity with DNS and configuring a Custom Domain Name
* Familiarity with creating a REST API
* Have a paid subscription to ChatGPT

### Background Story
In my quest to build GPTs with authentication, I found the lack of examples disheartening. Despite extensive searches on Perplexity, Arc Search, and ChatGPT, the closest resources were:
* [Authenticate users in GPT actions: Build a personal agenda assistant](https://blog.logto.io/gpt-action-oauth/) - Too closely tied to Logto.io
* [How to add OAuth authorization for custom GPTs](https://agilemerchants.medium.com/how-to-add-oauth-authorization-for-custom-gpts-d1eaf32ee730) - Using PHP, Laravel, and looks overly complicated
* [GPT Action with Google OAuth\)](https://www.youtube.com/watch?v=nA4FtyAKhfA) - Limited to Google APIs
* [GPT-Actions: GPT Auth](https://github.com/Anil-matcha/GPT-Actions) - Not using OAuth2  

Since I couldn’t find what I needed, I decided to create an example myself, hoping it would help others in similar situations.

### The Challenge
After getting the basics working and configuring the GPT, I faced a frustratingly generic error when trying to save:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/obckpnhetfe9w8jh5yiq.png)


To troubleshoot, I built another Custom GPT using OAuth2 to access my Google Calendar, confirming the issue was specific to my project. The community shared similar frustrations:
["Error saving draft" when creating an authenticated action in a GPT](https://community.openai.com/t/error-saving-draft-when-creating-an-authenticated-action-in-a-gpt/490733/14)

### The Solution
The breakthrough came from an unusual requirement in the OpenAI docs: the OAuth2 server must share the same domain as the API server, except for Google, Microsoft, and Adobe OAuth domains.

Once I configured a custom domain on Clerk, everything worked beautifully! The result is a template project that any developer can fork, customize, and deploy in a single day.
	* 	[GitHub Repository: Custom GPT API OAuth](https://github.com/jfbloom22/custom-gpt-api-oauth)


### Demo
Check out the demo for a hands-on experience. Feel free to explore, and even spam the database - restoring it is easy with backups on Neon.

[Demo: My Pizza Dough - OAuth Demo](https://chatgpt.com/g/g-oXRAoqOK8-my-pizza-dough-oauth-demo)

**Checkout the rest of my series on GPTs: ~[Who Cares About Custom GPTs?](https://blog.jonathanflower.com/uncategorized/who-cares-about-custom-gpts/)~**