GPT-4 for API design research

I came up with a really useful prompt for GPT-4 today. I was considering options for refactoring how Datasette's core view functions work, and was contemplating alternative ways to dispatch to different functions based on a combination of the URL path and the HTTP verb.

I decided it would be useful to see how other frameworks solved that, and came up with the following prompt:

Show me code examples of different web frameworks in Python and JavaScript and Go illustrating how HTTP routing works - in particular the problem of mapping an incoming HTTP request to some code based on both the URL path and the HTTP verb

Don't show installation instructions, just get straight to the code snippets

(I added that second line after it first started spitting out installation instructions for each framework, when I just wanted to see some illustrative code examples.)

I got really great results!

It started by kicking out code examples for Flask (Python), Express (JavaScript/Node.js) and Gin (Go).

Then I prompted:

Three more examples for the same languages but showing different frameworks

It gave me Django (Python), Koa (JavaScript/Node.js) and Echo (Go).

Finally, I added:

Some Rust examples too

And it gave me examples for Actix-web and Rocket, both in Rust.

I've shared the full transcript here:

As someone who spends a whole lot of time on API design, this feels like a really useful trick - researching how multiple frameworks across multiple other languages solve a problem is incredibly useful for coming up with a coherent new API design.

The examples it gave me

Here's just the code from the examples I found most useful:


from flask import Flask

app = Flask(__name__)

@app.route('/example', methods=['GET'])
def get_example():
    return "This is a GET request."

@app.route('/example', methods=['POST'])
def post_example():
    return "This is a POST request."

if __name__ == '__main__':


const express = require('express');
const app = express();

app.get('/example', (req, res) => {
  res.send('This is a GET request.');
});'/example', (req, res) => {
  res.send('This is a POST request.');

app.listen(3000, () => {
  console.log('Server is running on port 3000');


package main

import (

func main() {
	r := gin.Default()

	r.GET("/example", func(c *gin.Context) {
		c.String(200, "This is a GET request.")

	r.POST("/example", func(c *gin.Context) {
		c.String(200, "This is a POST request.")



use actix_web::{web, App, HttpResponse, HttpServer, Responder};

async fn get_example() -> impl Responder {
    HttpResponse::Ok().body("This is a GET request.")

async fn post_example() -> impl Responder {
    HttpResponse::Ok().body("This is a POST request.")

async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
            .route("/example", web::get().to(get_example))
            .route("/example", web::post().to(post_example))


const Koa = require('koa');
const Router = require('koa-router');

const app = new Koa();
const router = new Router();

router.get('/example', (ctx) => {
  ctx.body = 'This is a GET request.';
});'/example', (ctx) => {
  ctx.body = 'This is a POST request.';


app.listen(3000, () => {
  console.log('Server is running on port 3000');


from django.http import HttpResponse
from django.views import View

class ExampleView(View):
    def get(self, request):
        return HttpResponse("This is a GET request.")

    def post(self, request):
        return HttpResponse("This is a POST request.")
from django.urls import path
from .views import ExampleView

urlpatterns = [
    path('example/', ExampleView.as_view(), name='example'),


#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use] extern crate rocket;

use rocket::response::content;

fn get_example() -> content::Plain<&'static str> {
    content::Plain("This is a GET request.")

fn post_example() -> content::Plain<&'static str> {
    content::Plain("This is a POST request.")

fn rocket() -> _ {
    rocket::build().mount("/", routes![get_example, post_example])

And from a tip from Jim Flanagan, here's what it gave me for Elixir/Phoenix:

defmodule ExampleAppWeb.Router do
  use ExampleAppWeb, :router

  pipeline :api do
    plug :accepts, ["json"]

  scope "/api", ExampleAppWeb do
    pipe_through :api

    get "/example", ExampleController, :get_example
    post "/example", ExampleController, :post_example

Created 2023-04-06T12:17:57-07:00, updated 2023-04-06T12:41:20-07:00 · History · Edit