Course lesson

Create Astro API Endpoints to Get and Update Database Records using Drizzle ORM

Now that we have our database set up, we need to use it!

Duration
12 min
Access
Included
Transcript
Retained from source evidence

Now that we have our database set up, we need to use it!

On our blog post page, we have a likes button waiting for us to implement the likes feature. On page load, we'll make a fetch call to a /api/likes/[slug].ts GET end point to return the number of likes for the page.

Similarly, when someone clicks the like button we want to send a POST request to that same endpoint.

On the server side we will set up GET and POST handlers to implement this functionality. In those functions we'll use Drizzle to return the likes for that blog post and in the case of a POST we'll create a record if it doesn't exist or increment a like if it does and return.

Astro API endpoints utilize browser Response API so you'll get a chance to handle returning different status's based on the data present.