Developing a Custom ChatGPT Plugin with Express.js: A Comprehensive Guide
Introduction to ChatGPT Plugins
ChatGPT plugins are a powerful tool that allows developers to extend the capabilities of ChatGPT by integrating it with external APIs, databases, or other services. These plugins can be used to create more interactive and dynamic conversational experiences, enhancing the functionality and versatility of ChatGPT.
In this guide, I'll walk you through the process of creating a custom ChatGPT plugin that interacts with the API-Ninja's API to fetch airport data based on a city name provided by the user. This serves as a simple yet functional example to demonstrate the potential of ChatGPT plugins.
Getting Started with ChatGPT Plugin Development
Once you have cloned the repository, you will need to install the necessary dependencies. After that, you will need to edit the
.env.sample file to add your API Ninjas API key and then rename it to
Finally, you can serve the plugin using the command
node index. The plugin will now be running on
For a smoother development process, I recommend utilizing Nodemon for your server applications. This tool offers the convenience of automatic server restarts whenever changes are saved, enhancing your efficiency and productivity.
Understanding How a ChatGPT Plugin Works
The operation of a ChatGPT plugin can be broken down into four key components:
An application: This is the part of the plugin that performs a specific function. This could range from executing an API call to retrieve data or interact with other services to implementing various types of logic, such as performing arithmetic operations or even handling more complex tasks.
A server: This is the part of the plugin that facilitates communication with your application and invokes its functions. ChatGPT interacts with the plugin through this server.
OpenAPI Schema: This is a comprehensive documentation of the available server endpoints. ChatGPT uses this schema to understand the plugin's capabilities and determine when to call a specific endpoint.
Plugin manifest: This is a JSON file that contains detailed information about the plugin. It provides ChatGPT with a clear understanding of the plugin's purpose and functionality and the URLs needed to locate the server and the logo.
The Structure of a ChatGPT Plugin
The fundamental structure of a ChatGPT plugin includes several key files and directories:
.well-known/ai-plugin.json: This is the plugin manifest.
src/app.js: This is the main application file.
.env: This is the environment variables file.
index.js: This is the main server file.
openapi.yaml: This is the OpenAPI schema.
logo.png: This is the logo of the plugin.
Each of these components plays a crucial role in the operation of the plugin, and they can be tailored to meet your specific requirements.
Make sure the check the ChatGPT quickstart repository to find all the code.
Developing the Application and the Server
In our example, the application is a simple Node.js module that fetches airport data for a given city using the API Ninjas Airports API. The server is an Express.js server with various endpoints that serve the ChatGPT plugin.
The application and the server are the heart of the plugin, enabling ChatGPT to establish a connection, locate the manifest and OpenAPI schema to comprehend its functionality, and ultimately access the endpoints that interact with the application.
The Role of OpenAPI Schema in ChatGPT Plugins
The OpenAPI schema, also known as an OpenAPI specification, is a powerful tool for describing and documenting APIs. It's a standard, language-agnostic specification for RESTful APIs, which allows both humans and computers to understand the capabilities of a service without needing to access the source code, additional documentation, or network traffic inspection.
In an OpenAPI schema, you define all the aspects of your API. This includes:
Endpoints (Paths): These are the routes or URLs where your API can be accessed.
Operations: These are the actions that can be performed on each endpoint, such as GET, POST, PUT, DELETE, etc.
Parameters and Request Body: These define what data can be passed to the API.
Response: This defines what the API returns after an operation.
Models (Schemas): These are definitions of the data structures that the API uses.
By using an OpenAPI schema, developers can understand how to use your API without having to read through all your code or rely on extensive external documentation.
Understanding the Plugin Manifest
The manifest file is a JSON file that provides essential information about the plugin to ChatGPT. It includes details like the version of the schema that the plugin is using, the name of the plugin as it should be displayed to humans and referred to by the model, a description of the plugin for humans and the model, the type of authentication required by the plugin, information about the API used by the plugin, and the URLs needed to locate the server and the logo.
Whether you're looking to integrate ChatGPT with external APIs, databases, or other services, developing custom plugins can open up a world of possibilities for enhancing the functionality and versatility of ChatGPT. Happy coding!
Did you find this article valuable?
Support Davide by becoming a sponsor. Any amount is appreciated!