Now we are going to add an API that returns a list of all the notes a user has. This’ll be very similar to the previous chapter where we were returning a single note.

Add the Function

Change indicator Create a new file in packages/functions/src/list.ts with the following.

import { Table } from "sst/node/table";
import handler from "@notes/core/handler";
import dynamoDb from "@notes/core/dynamodb";

export const main = handler(async (event) => {
  const params = {
    TableName: Table.Notes.tableName,
    // 'KeyConditionExpression' defines the condition for the query
    // - 'userId = :userId': only return items with matching 'userId'
    //   partition key
    KeyConditionExpression: "userId = :userId",
    // 'ExpressionAttributeValues' defines the value in the condition
    // - ':userId': defines 'userId' to be the id of the author
    ExpressionAttributeValues: {
      ":userId": "123",

  const result = await dynamoDb.query(params);

  // Return the matching list of items in response body
  return JSON.stringify(result.Items);

This is pretty much the same as our get.ts except we use a condition to only return the items that have the same userId as the one we are passing in. In our case, it’s still hardcoded to 123.

Add the Route

Let’s add the route for this new endpoint.

Change indicator Add the following above the POST /notes route in stacks/ApiStack.ts.

"GET /notes": "packages/functions/src/list.main",

Deploy Our Changes

If you switch over to your terminal, you will notice that your changes are being deployed.

You should see that the new API stack has been deployed.

✓  Deployed:

Test the API

Let’s test list all notes API.

Change indicator Run the following in your terminal.

$ curl

Again, replacing the example URL with your ApiEndpoint value.

Since we are making a simple GET request, we could also go to this URL directly in your browser.

The response should look something like this.

[{"attachment":"hello.jpg","content":"Hello World","createdAt":1629336889054,"noteId":"a46b7fe0-008d-11ec-a6d5-a1d39a077784","userId":"123"}]

Note that, we are getting an array of notes. Instead of a single note.

Next we are going to add an API to update a note.