---
title: "How to build and authenticate an AI API using Modal, Neon, and Unkey"
description: "An OpenAI Whisper transcription API built with FastAPI and deployed on Modal, which persists transcripts to a Neon serverless Postgres database via psycopg2."
url: "https://swe.quest/articles/how-to-build-and-authenticate-an-ai-api-using-modal-neon-and-unkey-83191f2d4ca23270"
canonical: "https://neon.com/blog/how-to-build-and-authenticate-an-ai-api-using-modal-neon-and-unkey"
source: "Neon"
published: "2024-02-14"
difficulty: "medium"
kind: "How-to"
reading_minutes: 6
tags: ["serverless","identity-auth","api-design","python","postgres"]
---

# How to build and authenticate an AI API using Modal, Neon, and Unkey

A post on [Neon](https://swe.quest/sources/neon.md), published 2024-02-14, 6 min read, How-to. Original: [https://neon.com/blog/how-to-build-and-authenticate-an-ai-api-using-modal-neon-and-unkey](https://neon.com/blog/how-to-build-and-authenticate-an-ai-api-using-modal-neon-and-unkey)

## Summary

An OpenAI Whisper transcription API built with FastAPI and deployed on Modal, which persists transcripts to a Neon serverless Postgres database via psycopg2. Requests are gated by API keys verified through Unkey's Python SDK. The walkthrough covers defining the Modal image and stub, writing uploads to a temp file for Whisper, creating the database and table with neonctl and psql, passing the connection string as a Modal secret, and checking the Authorization header against Unkey before returning a permanent link to the transcript.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Serverless](https://swe.quest/tags/serverless.md) (Backend & APIs)
- [Identity & auth](https://swe.quest/tags/identity-auth.md) (Security)
- [API design](https://swe.quest/tags/api-design.md) (Backend & APIs)
- [Python](https://swe.quest/tags/python.md) (Languages)
- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)

## Prerequisites

- Python web APIs ([easy posts on Python](https://swe.quest/tags/python.md?difficulty=easy))
- FastAPI basics
- PostgreSQL basics ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))

## Keywords

Modal, Neon, Unkey, OpenAI Whisper, FastAPI, psycopg2

## Related posts

- [Deploy a Serverless FastAPI App with Neon Postgres and AWS App Runner at any scale](https://swe.quest/articles/deploy-a-serverless-fastapi-app-with-neon-postgres-and-aws-app-runner-at-any-6068dfbf055e1e25.md): Neon · 2024-02-09 · Medium · 11 min read · Python, PostgreSQL, Serverless, Cloud platforms, Deployment
  A step-by-step setup for a Python API built with FastAPI and SQLModel, backed by Neon serverless Postgres and deployed to AWS App Runner with automatic deploys on each Git push.
- [Neon for Agent Platforms: A Companion Skill for the AI Agent Program](https://swe.quest/articles/neon-for-agent-platforms-a-companion-skill-for-the-ai-agent-program-6b42423040c5c41e.md): Neon · 2026-05-22 · Medium · 3 min read · PostgreSQL, Multi-tenancy, AI agents, Cloud platforms
  neon-for-agent-platforms is an open-source companion Agent Skill and set of TypeScript scripts for running fleets of Neon Postgres databases on agent platforms.
- [Storing Vercel's AI SDK chat messages in a Turso database](https://swe.quest/articles/storing-vercel-s-ai-sdk-chat-messages-in-a-turso-database-14cd5acbb753b2cf.md): Turso · 2024-08-07 · Medium · 5 min read · SQLite, LLMs, React, TypeScript
  Vercel AI SDK's useChat hook doesn't persist conversations, so chat history can be stored in a Turso (libSQL) table and reloaded on page render.
- [Building an AI-powered ChatBot using Vercel, OpenAI, and Postgres](https://swe.quest/articles/building-an-ai-powered-chatbot-using-vercel-openai-and-postgres-4fd423473384581f.md): Neon · 2023-03-09 · Medium · 13 min read · RAG & retrieval, PostgreSQL, Vector search, LLMs, Serverless
  A ChatGPT-style documentation assistant (Ask Neon) is built by storing OpenAI text-embedding-ada-002 vectors in Postgres with pgvector, retrieving context by cosine distance, and feeding it to text-davinci-003 from a Vercel Edge Function.
- [Building a GPT Backed by a Heroku-Deployed API](https://swe.quest/articles/building-a-gpt-backed-by-a-heroku-deployed-api-48a6672b2eb1e607.md): Heroku Engineering · 2024-03-28 · Medium · 15 min read · AI agents, LLMs, JavaScript, PostgreSQL
  A custom OpenAI GPT can be backed by a simple Express API on Heroku that answers natural-language questions about an employee directory.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
