Skip to content
 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OpenAI API Quickstart with FastAPI

A FastAPI port of openai/openai-quickstart-python. Check out the examples folder to try them out.

Example What it shows
examples/pet-name A form that generates superhero pet names, using the Responses API
examples/chat A multi-turn chat that streams tokens as they arrive, using Chat Completions

Setup

  1. If you don’t have Python installed, install it from Python.org.

  2. Clone this repository.

  3. Navigate into the project directory:

    $ cd openai-quickstart-fastapi
  4. Create a new virtual environment:

    • macOS / Linux:

      $ python -m venv venv
      $ . venv/bin/activate
    • Windows:

      > python -m venv venv
      > .\venv\Scripts\activate
  5. Install the requirements:

    $ pip install -r requirements.txt
  6. Make a copy of the example environment variables file:

    $ cp .env.example .env
  7. Add your API key to the newly created .env file.

  8. Run an example:

    $ cd examples/pet-name
    $ fastapi dev app.py

You should now be able to access the app from your browser at http://localhost:8000!

demo

Using uv

Steps 4 and 5 with uv instead:

$ uv venv
$ uv pip install -r requirements.txt

Choosing a model

Both examples read OPENAI_MODEL, so you can try another model without editing the code:

$ OPENAI_MODEL=gpt-5-mini fastapi dev app.py

The SDK also reads OPENAI_BASE_URL, which lets the chat example run against any OpenAI-compatible endpoint. Note that the pet name example uses the Responses API, which fewer providers implement.

Docker

Builds the pet name example:

$ docker build -t openai-quickstart-fastapi .
$ docker run -e OPENAI_API_KEY=<your-api-key> -p 8000:8000 openai-quickstart-fastapi

Reference

About

OpenAI and FastAPI - Python example app

Topics

Resources

Stars

30 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages