Build an API function with ChatGPT
Generate the JSON payload of a function with the ChatGPT integration of the Azion Console editor, then read the response with Preview deployment.
You build a function that answers a GET request with HTML and a POST request with JSON. The ChatGPT integration of the Azion Console code editor writes the JSON payload from a comment.
Two neighboring pages own the parts this one leaves out:
- For the handler patterns of a function and its log output, refer to Write and test a function.
- For an API backed by a database, refer to Build a RESTful API with Functions and SQL Database.
Prerequisites
- An Azion account. To create one, refer to How to create an account on Azion.
- The Edit Functions permission on the account. It also requires the permission View Functions. Refer to Teams Permissions.
- An account on OpenAI, with an API key and an organization ID.
Declare the OpenAI credentials
The integration runs on the credentials of your own OpenAI account. It reads them from a comment in the source code of the function, so the credentials come first.
To put the credentials in the source code of a new function:
Access Azion Console > Products Menu > Libraries > Functions.
Enter a name for the function. For example: chatgpt-api.
In the Code tab, replace the placeholder code with the two values of your OpenAI account:
The editor commands of the integration now authenticate with your OpenAI account.
Generate the response data with ChatGPT
Code generation turns a comment into JavaScript. The ChatGPT: Generate command reads the selected comment as the prompt and writes the code it returns.
To generate the object the API returns:
Below the credentials, enter the comment that describes the object:
The request takes a few seconds.
The integration writes the object in the editor. The output is similar to:
The source code now carries the object the API returns for a POST request.
Assemble the handler
The generated object is data. The handler decides what the function returns for each request method. A fetch handler branches on request.method: the markup for a GET, the object for a POST.
Complete the source code with the HTML constant, the handler, and the default export:
Each branch declares the format it returns in the content-type header:
- The
GETcase returns thehtmlconstant astext/html;charset=UTF-8. - The
POSTcase returns the generated object asapplication/json.
The switch covers GET and POST. Add a case for every other method the API answers.
The default export is the ES Modules pattern, the recommended way to structure a function. To move existing code off the legacy Service Worker pattern, refer to Migrate handler patterns in Functions.
The source code now holds the complete handler of the API.
Preview the response
Preview deployment runs the function in Azion Console and shows the response the code returns. It calls PreviewProvider, an auxiliary function in the source code that builds a simulated request for the handler.
To read both responses before the function serves traffic:
PreviewProvider builds the request the preview sends and returns the response the preview renders. Add it below the default export, with GET as the method:
For every value the simulated request carries, refer to PreviewProvider function.
The preview renders the text/html response as the page. Select Open to read the same output in a separate browser tab.
The preview renders the application/json response as the JSON body of the generated object.
Both responses of the function appear in Azion Console, with no deployment.
Save the function
The complete source code carries the credentials, the generated object, the handler, and the preview function:
To store the code, select Save.
Azion Console stores the function and makes it available to instantiate on an application. The function serves no traffic until an instance binds it and a Rules Engine rule selects that instance.