Generative Development Framework
GDF.ai
  • Intro to GDF-FSE
    • Generative AI, Large Language Models, ChatGPT?
    • Knowledge Areas
    • Access a Chat Based LLM
    • Why GDF?
    • Expectations
  • Limitations
  • Prompting
    • Prompt Patterns
    • Prompt Context
    • Prompt Stores
    • Prompt Operators
    • Prompt Chaining
  • Security
    • Protecting Data
    • Protecting Application Security
    • Protecting Intellectual Property
    • Protection Stores
    • AI Security Assessments and Penetration Testing
    • Social Engineering Testing with AI
  • Subject Knowledge Areas
    • Ideation
      • Identifying a Problem Statement
      • Plan and Prioritize Features
      • Develop User Stories
      • Requirement Gathering
      • Ideation Prompting
      • Ideation Template
    • Specification
      • Specifying Languages
      • Specifying Libraries
      • Specifying Project Structures
      • Specify Schemas
      • Specifying Elements
      • Specifying API Specs
    • Generation
      • Generating UI Elements
      • Generating Mock Data
      • Generating Schemas
      • Generating Parsers
      • Generating Databases
      • Generate Functions
      • Generate APIs
      • Generate Diagrams
      • Generating Documentation
    • Transformation
      • Converting Languages
      • Converting Libraries
    • Replacement
      • Replacing Functions
      • Replacing Data Types
    • Integration
      • Connecting UI Components
      • Connecting UI to Backend
      • Connecting Multiple Services Together
      • Connecting Cloud Infrastructure (AWS)
    • Separation
      • Abstraction
      • Model View Controller (MVC)
    • Consolidation
      • Combining UI Elements
      • Deduplicating Code Fragments
    • Templating
      • Layouts
      • Schemas
      • Project Structures
      • Content Management Systems
    • Visualization
      • General Styling
      • Visual Referencing
      • Visual Variations
    • Verification
      • Test Classes
      • Logging and Monitoring
      • Automated Testing
      • Synthetic Monitoring
    • Implementation
      • Infrastructure
      • DevOps / Deployment
    • Optimization
      • General Optimization
      • Performance Monitoring
      • Code Review
  • Guidance
    • Business Process
    • Regulatory Guidance
  • Generative Pipelines
  • Troubleshooting
    • Client Side Troubleshooting
    • Server Side Troubleshooting
    • Troubleshooting with AI
    • Documentation
    • Infrastructure Engineering
  • Terminology
Powered by GitBook
On this page

Was this helpful?

Export as PDF
  1. Subject Knowledge Areas
  2. Specification

Specifying API Specs

How to specify API specifications using generative AI

APIs (application programming interfaces) are applications that orchestrate calls and responses between systems.

Consider an instant message on an application like WhatsApp. When the user sends a message, it is going to an url (endpoint) that might be something like https://apis.whatsapp.com/messenger/thread/{threadId}. The message is received by this API and then written into a database.

Creating an OpenAPI specification for your bicycle rental application API middleware is a great way to establish clear documentation and generate boilerplate code for your Node.js Express server. In this article, we will walk through the process of creating an OpenAPI specification and then generating a Node.js Express application based on that specification.

Step 1: Design the OpenAPI Specification

Start by designing the OpenAPI specification for your bicycle rental application. The specification should define the API's endpoints, request and response formats, and any necessary authentication or authorization details. Here's a simple example of an OpenAPI specification for a bicycle rental application:

chatgpt-4
create an openapi spec for a bicycle rental api
openapi: 3.0.0
info:
  title: Bicycle Rental API
  version: 1.0.0

paths:
  /bicycles:
    get:
      summary: Get a list of available bicycles
      responses:
        '200':
          description: Successfully retrieved the list of bicycles
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Bicycle'
  /bicycles/{bicycleId}:
    get:
      summary: Get the details of a specific bicycle
      parameters:
        - name: bicycleId
          in: path
          required: true
          schema:
            type: string
      responses:
        '200':
          description: Successfully retrieved the bicycle details
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Bicycle'

components:
  schemas:
    Bicycle:
      type: object
      properties:
        id:
          type: string
        name:
          type: string
        type:
          type: string
        pricePerHour:
          type: number

Save the specification in a file named bicycle-rental-api.yaml.

Below I have imported the spec into the documentation to demonstrate that the created spec compiles successfully.

Step 2: Generate the Node.js Express Code

First, install the OpenAPI Generator CLI:

npm install -g @openapitools/openapi-generator-cli

Next, generate the Node.js Express code:

openapi-generator generate -i bicycle-rental-api.yaml -g nodejs-express-server -o ./bicycle-rental-api

This command will create a new directory named bicycle-rental-api, containing the generated Node.js Express code based on your OpenAPI specification.

Step 3: Set Up and Run the Generated Express Server

Navigate to the bicycle-rental-api directory and install the required dependencies:

cd bicycle-rental-api
npm install

Now, run the generated Express server:

npm start

Your Express server should now be running, and you can access the API endpoints as defined in your OpenAPI specification.

Conclusion

By creating an OpenAPI specification for your bicycle rental application API middleware and using a code generation tool, you can establish clear documentation and kickstart your Node.js Express server development. As you continue to develop your application, you can update the OpenAPI specification to reflect changes or additions to your API and regenerate the server code as needed.

PreviousSpecifying ElementsNextGeneration

Last updated 3 months ago

Was this helpful?

Note the above is a very simple API specification. You could use prompt patterns from the , , and knowledges areas to build out a production grade API spec.

To generate the Node.js Express code based on your OpenAPI specification, you can use tools like or . In this example, we'll use the OpenAPI Generator.

replacement
transformation
generation
OpenAPI Generator
swagger-node-codegen

Get a list of available bicycles

get
Responses
200
Successfully retrieved the list of bicycles
application/json
get
GET /bicycles HTTP/1.1
Host: 
Accept: */*
200

Successfully retrieved the list of bicycles

[
  {
    "id": "text",
    "name": "text",
    "type": "text",
    "pricePerHour": 1
  }
]

Get the details of a specific bicycle

get
Path parameters
bicycleIdstringRequired
Responses
200
Successfully retrieved the bicycle details
application/json
get
GET /bicycles/{bicycleId} HTTP/1.1
Host: 
Accept: */*
200

Successfully retrieved the bicycle details

{
  "id": "text",
  "name": "text",
  "type": "text",
  "pricePerHour": 1
}
  • Step 1: Design the OpenAPI Specification
  • GETGet a list of available bicycles
  • GETGet the details of a specific bicycle
  • Step 2: Generate the Node.js Express Code
  • Step 3: Set Up and Run the Generated Express Server
  • Conclusion