ChatGPT API
ChatGPT API
● We can access ChatGPT using the web interface and a browser
● But how do other programs incorporate ChatGPT usage?
● ChatGPT has what is called an Application Programming Interface (API)
● We can say that APIs are a set of rules and specifications that allow different
software systems to communicate and interact with one another
● ChatGPT’s API is a type of Web API that allows us to access ChatGPT
functionalities over the Internet
Web APIs
● There are many Web APIs that provide different services and information
○ e.g. cryptocurrency prices
○ e.g. payment processing services such as PayPal and Stripe
● A list of publicly available APIs:
○ [Link]
● Web APIs are typically accessed via HTTP requests
○ An HTTP request is what occurs when you enter a URL into the browser and hit enter
○ A request occurs over the Internet to a server, which is another machine connected to the
Internet
○ The server would then send a response to your computer/browser
○ The browser than renders the content in the response
HTTP requests
● HTTP stands for Hypertext Transfer Protocol, it’s the protocol that allows web
traffic to occur
● In general, a protocol is a system of rules that allow communication and
information transmission to occur
● Other protocols besides HTTP exist, and at different levels with different
purposes, as part of the Internet protocol suite known as TCP/IP
● A secure version called HTTPS that encrypts communication is more
commonly used today, but for simplicity we’ll just refer to “HTTP requests”
HTTP requests
● HTTP requests come in different “verbs”, which are essentially different types
of requests that are intended for different purposes
● GET, POST, PUT, DELETE and other request verbs
● HTTP requests work in request-response cycle
● A request is sent to a server, the server receives the requests and then sends
a response
HTTP request-response cycle
HTTP requests and responses
● HTTP requests and responses can include different types of header data
● Header data is basically metadata, data about the request or response
such as what type of data is being transmitted
○ HTTP headers are key-value pairs, for example Content-Type: text/html
● HTTP requests and responses can include a request body or response body,
i.e. the actual data being exchanged back and forth
○ The data could be plaintext, binary data for images, or JSON data
○ These request and response bodies are one way in which data can be sent to and received
from a web service
HTTP requests
● An HTTP request can also include key-value query parameters as a way of
sending data along with a request
● [Link]
● A request to the above URL would send the query parameter customer set to
kevin and deposit set to 1000
● The server would then receive this information, and could act on the specific
values provided… e.g. depositing 1000 for the user kevin
Web API Endpoints
● Web API Endpoints are URLs where particular web services are accessible
● A single web API may have multiple endpoints, with each endpoint for
delivering a different service
○ e.g. one for payments, one for refunds, etc.
● Using a web API is a little bit like calling functions from a library
○ Instead of calling a function, we make a request to an endpoint
○ Instead of passing arguments, we pass data in with headers, query parameters and/or the
request body
○ Instead of getting a return value, we’ll receive a response with a response body
JSON
● Many modern web APIs work by exchanging JSON data in request/response bodies
○ XML data is sometimes used, and was originally the more popular format
● JSON stands for JavaScript Object Notation
○ It’s the notation for defining objects in JavaScript, because so much of the web relies on JavaScript it
makes a convenient data exchange format
● JSON data can have objects made up of key-value pairs:
{
firstName: “Kevin”,
lastName: “Browne”
}
JSON
● JSON data can have arrays of elements:
[1,2,3,4,5]
● JSON data can have different types of values such as as strings, numbers
and booleans
● We can have arrays of objects, and objects can have arrays as values for
keys, allowing us to express structured data very nicely
JSON Data Example
{
status: “OK”,
total: 3,
results: [ {name: “Math”, average: 95.6},
{name: “English”, average: 94.3},
{name: “Biology”, average: 68.4} ]
}
API Keys
● The types of services that APIs provide are not free for the providers
● Servers and data storage are required
● Applications that do work to provide services need to be written
● Computation, electricity and data transmission all have costs as well
● Some APIs are created for profit, others are maintained on a non-profit basis,
it depends on the business model
API Keys
● API providers will often require an API key to access the service
● The key is unique to each application or user, and can be sent along with the
request in some form or another, perhaps in an HTTP header
● The server will receive the key along with the request, and then charge the user of
that key some amount of money for the amount of service they’ve used
● This way the more someone uses a web API, the more the provider can make
money :-)
● It’s important we keep our API keys secret, otherwise other people could use them
ChatGPT API
● To use the ChatGPT API we’ll need to:
○ Create a Chat GPT account
○ Create an API key and keep it after we create it
○ Purchase some credits
○ Send the API key along with our requests
● It’s possible that you when sign-up you will be given some amount of free
credits to be used, that depends on OpenAI
● But you may need to register a payment method and purchase some credits
○ e.g. start with something small like $5 in credits
○ The exact cost per input and output to ChatGPT varies per model and over time