How to turn your API into a GPT

Alex Gelman
CyberArk Engineering
4 min readJan 14, 2024

OpenAI recently introduced the custom GPT store which allows you to create your own customized GPTs that let you provide your own instructions, knowledge base or API actions.
With the new store, you can also share your custom GPT with other ChatGPT users. In this post you’ll learn how to turn your existing APIs into a GPT that can be shared with others through the store in just a few simple steps.

Step 1: Preparing Your API

First, you’ll need an OpenAPI spec of our API. For simplicity, I’ll use a free demo API available through this list here. The API supports two actions that list hot and cold coffee drinks.

The OpenAPI spec file for the API is:

openapi: 3.0.0
info:
title: Coffee API
version: 1.0.0
servers:
- url: https://api.sampleapis.com/coffee
paths:
/hot:
get:
summary: List all hot coffee drinks
description: Returns a list of all the hot coffee drinks available
operationId: getHotCoffee
responses:
'200':
description: A list of hot coffee drinks
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Coffee'
/cold:
get:
summary: List all cold coffee drinks
description: Returns a list of all the cold coffee drinks available
operationId: getColdCoffee
responses:
'200':
description: A list of cold coffee drinks
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Coffee'
components:
schemas:
Coffee:
type: object
properties:
id:
type: integer
title:
type: string
description:
type: string
ingredients:
type: array
items:
type: string
image:
type: string

Step 2: Creating a Custom GPT

Now that we have our OpenAPI spec, let’s create a custom GPT.

Here’s how:

  • Go to OpenAI’s Chat GPT.
  • In the left-hand pane, select Explore GPTs.
  • In the top right corner select Create.
  • Select Configure to directly set the custom GPT properties.

Now let’s configure our new GPT to help our users get started with the available API.

We’re going to call the new GPT Barista GPT and give it simple instructions with the following prompt:

“You manage lists of possible hot and cold coffee drinks, help the users find the drink they want”

Quick tip: Remember to disable web browsing and DALL-E for the GPT since you don’t need them.

Barista GPT configuration example

Step 3: Adding API Actions

Now for the most important part. We’re going to provide API actions that our GPT can use.

Here’s how:

  • Scroll down and select Create new action.
  • Under the schema field, paste the OpenAPI spec from above. If you have your own OpenAPI spec that is available online, select the Import from URL button to import it.
  • Once we paste the spec, OpenAI validates the spec and shows any errors that exist.
  • Save the GPT by selecting Save on the top right-hand side. You can either make it available to Only me (e.g., private) or you can choose to make it public which makes it available to everyone through the store.

That’s it! Your GPT is ready and you can use it to ask questions about hot and cold drinks.

Asking Barista GPT for hot coffee drinks that don’t contain milk

Using a custom GPT allows users to interact with APIs in natural language, converting the user question to API parameters and transforming the response from the APIs.
By integrating an API with GPT, you’re leveraging all the GPT capabilities with the API, and GPT can understand the data returned by the API and interact with it.

In the example above, I asked GPT to list only the hot coffee drinks that do not use milk.
GPT can understand my prompt, call the API to list hot drinks, filter the drinks based on the ingredients to those that don’t include milk, then format the JSON response into a list, and fetch the image from the URL.

Step 4: Authentication

The example above is using a simple and publicly open API.

But real-world APIs most likely require some kind authentication that is required before users are able to use the API. You can specify the authentication schema when configuring the actions for the GPT.

OpenAI supports two authentication schemas:

  • API key. If you choose to use an API key, you’ll need to provide the key value, this key will be used for all the users of your GPT.
  • OAuth. Provides more granular access to your API, but you need an identity provider to authenticate your users. Create an OAuth client in your identity provider and provide the client ID, client secret, authorization and token endpoints in the GPT authentication configuration.
    Lastly, define scopes to authorize the GPT to perform actions on the user’s behalf once they are authenticated.

A Better User Experience in Less Time

Using a GPT saves development time since GPT transforms inputs and outputs according to the user prompt without requiring us to develop dedicated APIs for every user’s needs.
Allowing our users to interact with the API through GPT can also improve the user experience in two ways. First, users can achieve their goals faster without writing their own scripts to interact with the API. Second, they no longer need UI for different use cases because GPT provides support for many use cases through natural language.

Now that you’ve seen how easy it is to take an existing API and turn it into a GPT that allows users to interact with an API and return data from it, I hope you have enough information here to start exploring this idea on your own.

--

--

Alex Gelman
CyberArk Engineering

Principal software architect @CyberArk and full stack tech geek