AI and APIs

An introduction to accessing and using the ChatGPT API

What is an API?


  • Application Programming Interface allows a user to access data from a remote server
  • A RESTful API (Representational State Transfer) is a common architecture that uses standard HTTP commands (GET, POST, …)

Laptop icons created by Freepik - Flaticon (flaticon.com/free-icons/laptop); Server icons created by Elias Bikbulatov - Flaticon (flaticon.com/free-icons/server); Automation icons created by Becris - Flaticon (flaticon.com/free-icons/automation)

Why use an API?


  • Query and retrieve remote data for a repeatable analysis (e.g., IUCN Red List data)
  • Query and retrieve remote data programatically for a website or Shiny App (e.g., The Phactory)
  • Build a package or function to allow others to access data for their own nefarious schemes

Laptop icons created by Freepik - Flaticon (flaticon.com/free-icons/laptop); Server icons created by Elias Bikbulatov - Flaticon (flaticon.com/free-icons/server); Automation icons created by Becris - Flaticon (flaticon.com/free-icons/automation)

Log in to your ChatGPT account


To use the API, you need an authorized account at https://auth.openai.com/log-in.

You can sign up with OpenAI or use your Google (or other) login credentials.

OpenAI Playground tab


The basic chat interface you’ve probably seen before:

OpenAI API documentation tab


Overview of the OpenAI API. We’ll come back to this later.

OpenAI Developer documentation tab


Instructions on accessing OpenAI API through the OpenAI Python library

OpenAI Settings tab


We need to set up an API key and put up a little credit to use the API

OpenAI Settings tab: billing


Add credit under Settings –> Billing (min $5, but expires after 1 year)

OpenAI Settings tab: API keys


Generate a new API key under Settings –> Billing