Skip to main content

How to create a Node.js REST API backend using ChatGPT and codehooks.io

ยท 7 min read
Martin

ChatGPT, a variant of the popular GPT-3 language model, is all the rage these days. People everywhere creates videos about it or blog about it. It's changing our world. And we understand why! ChatGPT is totally awesome for producing all kinds of text and answers and that includes code!

By providing ChatGPT with examples of codehooks.io backend code, we will in this blog post show you how you can use ChatGPT to produce codehooks JavaScript backend code that is both functionally correct, easy to understand and instantly deployable. In the end, we will have a tool that can help us quickly prototype and build different kinds of backends with minimal effort. So, let's get started!

socialcard

A database-powered CRUD REST API with ChatGPTโ€‹

We thought that ChatGPT could be a good match for creating code for codehooks.io due to codehooks' serverless and "instant deploy without setup" capability along with its built-in database, jobs and worker queue features.

One problem we figured out we might have was due to the fact that codehooks.io was released in late 2022 and ChatGPT has only been trained on data up to 2021. However, after experimenting a bit, we figured out a way to ask ChatGPT by providing an example instantly.

How to ask ChatGPT for a favor ๐Ÿ™โ€‹

If you haven't signed up for ChatGPT yet, you must do so before you can talk to it. To create our question for creating a CRUD REST API for deployment with codehooks.io using the codehooks.js/crudlify NPM package, we copied the following code from one of our blog posts:

/*
* Minimal CRUD API code example using Yup schema
*/
import {app, crudlify} from 'codehooks-js'
import * as yup from 'yup';

// "product" schema allowing any json object
const product = yup.object().shape({
json: yup.mixed()
})

// Add CRUD routes with yup schema for two collections
crudlify(app, {product})

// bind to serverless runtime
export default app.init()
Create database-backed REST APIs fast with crudlify โšก๏ธ

When you need a "standard" REST API with NoSQL database support, using the library 'codehooks.js/crudlify' must be one of the simplest ways to do it with code. The library generates the common REST API routes: GET, POST, PUT, PATCH and DELETE and persists the data in the codehooks.io NoSQL database. You can also query the data quite easily.

Then we added the following text before this code: "This is an example of codehooks.io backend code:", and we added this after the code: " Can you create a codehooks.io backend like this with a more complete example for an online shop."

The question we asked ChatGPT

And voilรก! This is the code we got back from ChatGPT:

import {app, crudlify} from 'codehooks-js'
import * as yup from 'yup';

// Define the schema for the "Product" collection
const productSchema = yup.object().shape({
name: yup.string().required(),
description: yup.string(),
price: yup.number().required(),
image: yup.string().url(),
category: yup.string().required(),
ratings: yup.array().of(yup.number())
});

// Define the schema for the "Order" collection
const orderSchema = yup.object().shape({
customerName: yup.string().required(),
customerEmail: yup.string().email().required(),
products: yup.array().of(yup.object().shape({
productId: yup.string().required(),
quantity: yup.number().required()
})),
total: yup.number().required(),
status: yup.string().oneOf(['pending', 'shipped', 'delivered'])
});

// Add CRUD routes for the "Product" and "Order" collections with the corresponding schemas
crudlify(app, { products: productSchema, orders: orderSchema });

// Bind the app to the serverless runtime
export default app.init();

This was the explanation ChatGPT was so kind to provide:

This code creates two collections for Product and Order, defined by the productSchema and orderSchema respectively. The schema of product includes name, description, price, image, category and ratings. The schema of order includes customerName, customerEmail, products, total, status.It also demonstrates the use of different validation techniques like required, email, url and oneOf() for various fields in the collection. It can be more fine-tuned based on the specific needs of the backend. Please note that this is just an example to give you an idea of how you can define your collections and schemas, and you may need to adjust it to fit the specific requirements of your application.

Deploying the AI-generated JavaScript code without touching it ๐Ÿ˜ฌโ€‹

This code certainly looks correct at first glance. I have, however, several times caught ChatGPT "inventing" totally new libraries and methods and got an excuse afterwards when I pinpointed it ๐Ÿ˜‚

Let's deploy the code! If you want to do this yourself, follow the instructions in our Quick Start to get ready. It only takes a few seconds.

First, create a new project:

codehooks create shopbackend

This CLI command creates a codehooks project, adds a folder named "shopbackend" and initializes a "hello world" index.js JavaScript file.

Replace the contents of the "index.js" file with ChatGPTs generated code above and save it.

You also need to install the necessary npm packages:

npm i codehooks-js codehooks-js yup

In the same folder as index.js, run the deploy command:

codehooks deploy

Result:

Project: shopbackend-h3r5  Space: dev
Deployed Codehook successfully! ๐Ÿ™Œ

Yes! ๐Ÿ™Œ

It worked...or rather it compiled and was deployed correctly by codehooks.io.

Let's test the backend by adding some products using cURL (we also recommend Postman).

Postman Import button

Using the "Import" button in Postman, you can directly import cURL commands.

This 'info' command is very useful when we need more information about our space AND need some cURL examples:

codehooks info --examples

This is what we get:

Project name: shopbackend-h3r5
Team: Martin Tornes (personal account)

API endpoint: https://shopbackend-h3r5.api.codehooks.io/dev

Examples in cURL:

curl https://shopbackend-h3r5.api.codehooks.io/dev/myroute -H 'x-apikey: 27b3822e-ecf3-4263-bcee-24acd6aed2d0' -H 'Content-Type: application/json'

curl --request POST https://shopbackend-h3r5.api.codehooks.io/dev/myroute -H 'x-apikey: 27b3822e-ecf3-4263-bcee-24acd6aed2d0' -H 'Content-Type: application/json' --data-raw '{"name": "Mr. Code Hooks", "active": true }'

Spaces:
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Name โ”‚ Tokens โ”‚ Allowed Hosts โ”‚ Jwks โ”‚ Env โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ dev (active) โ”‚ 27b3822e-ecf3-4263-bcee-24acd6aed2d0 (RW) โ”‚ โ”‚ โ”‚ โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Project members:
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ name โ”‚ email โ”‚ role โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Martin Tornes โ”‚ [email protected] โ”‚ ADMIN โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Let's modify the POST cUrl to add some product data. We'll add a JSON structure with at least the fields ChatGPT marked as required:

curl --request POST https://shopbackend-h3r5.api.codehooks.io/dev/products \
-H 'x-apikey: 27b3822e-ecf3-4263-bcee-24acd6aed2d0' \
-H 'Content-Type: application/json' \
--data-raw '{"name": "Codehook", "price": 0, "category": "TOOLS" }'

Result:

{"name":"Codehook","price":0,"category":"TOOLS","_id":"185a3157a34-u96ow26u9zcxd0"}

It worked! You can now use your preferred programming language and library to interact with this REST API and database.

Conclusionโ€‹

codehooks.io is a good fit for getting a lot of help from ChatGPT, you just need to show it an example before you ask a question. The way we demonstrated it here works fine, but there are probably multiple ways to get the most out of it.

We also asked it to create a scheduled "CRON" job using codehooks.io job "hooks", which should Tweet each afternoon at 5pm. It produced working code using Twitter's library and the correct codehooks.io JavaScript code. Ready to deploy!

We could have asked it to do more to elaborate on the code it created. This type of dialog is in fact the recommended way to work with it to produce the best results. We'll leave that to a later blogpost or a whole dedicated section where we add ChatGPT examples.

ChatGPT is a glimpse into an exciting future of AI-assisted programming. It's not exactly NoCode, but it surely is automated and saves a ton of time.

This blog post was not written by ChatGPT (maybe it could have been ๐Ÿ˜‰)

tip

Check out our example of how to use the Open AI API with ChatGPT's little brother GPT-3 to build your own easy to use API.