Build a Comments API
In this tutorial, you will use D1 and Hono ↗ to build a JSON API that stores and retrieves comments for a blog. You will create a D1 database, define a schema, and wire up GET and POST endpoints that read from and write to the database.
- Sign up for a Cloudflare account ↗.
- Install
Node.js↗.
Node.js version manager
Use a Node version manager like Volta ↗ or nvm ↗ to avoid permission issues and change Node.js versions. Wrangler, discussed later in this guide, requires a Node version of 16.17.0 or later.
-
Create a new project named
d1-comments-apiby running:npm create cloudflare@latest -- d1-comments-apiyarn create cloudflare d1-comments-apipnpm create cloudflare@latest d1-comments-apiFor setup, select the following options:
- For What would you like to start with?, choose
Hello World example. - For Which template would you like to use?, choose
Worker only. - For Which language do you want to use?, choose
TypeScript. - For Do you want to use git for version control?, choose
Yes. - For Do you want to deploy your application?, choose
No(we will be making some changes before deploying).
- For What would you like to start with?, choose
-
Move into the project directory:
Terminal window cd d1-comments-api
Install Hono ↗, a lightweight web framework for building APIs on Workers:
npm i hono yarn add hono pnpm add hono bun add hono -
Create a new D1 database with Wrangler:
Terminal window