useRestAPI

This is a default plugin that comes with the createHub function. This plugin will add the REST API endpoints for the collections you have defined in your HonoHub config. You can remove this if you want, check out the createBase function to see how to remove the default plugins, and create barebone HonoHub App.


Here is a list of the API endpoints that this plugins adds to your Hono app for each collection you have defined in your HonoHub config. You can use these endpoints to interact with your collections. All of these endpoints are validated using zod schemas generated using your Drizzle Schema, so you can be sure that the data you are sending is correct.

In these examples we will use the todos collection as an example and the base URL for the API will be /. You can replace todos with the name of your collection.

GET/collections/todos

List All Records

This endpoint will return all the records in the collection. You can enable pagination and filtering using query parameters.

Optional attributes

  • Name
    search
    Type
    string
    Description

    Search for records using a query string.

  • Name
    limit
    Type
    number
    Description

    The number of records to return per page. This shouldn't be more than the maxLimit defined in your collection schema.

  • Name
    offset
    Type
    number
    Description

    The number of records to skip.

  • Name
    sortBy
    Type
    string
    Description

    The field to sort the records by. You can use - to sort in descending order. For example, sortBy=-createdAt.

Response

Either returns all the records in an array if pagination is not enabled or an object with results and count keys if pagination is enabled.

Request

curl -L "http://localhost:3000/collections/todos?limit=3&offset=10"

Response

{
    "results": [
        {...},
        {...},
        {...},
    ],
    "count": 100,
}

POST/collections/todos

Create a Record

This endpoint will create a new record in the collection. You can send the data in the request body. The data will be validated using the schema defined in your Drizzle Schema by generating zod schemas.

Request

Send the data in the request body.

Response

Returns the newly created record. The output will be different for MySQL DB

Request

curl -L "http://localhost:3000/collections/todos" \
-H "Content-Type: application/json" \
-d "{
    \"status\": false,
    \"message\": \"The API is working\"
}"

Response

{
  "id": 4,
  "status": false,
  "message": "The API is working"
}

GET/collections/todos/count

Count Records

This endpoint will return the total number of records in the collection.

Response

Returns an object with the count key.

Request

curl -L "http://localhost:3000/collections/todos/count"

Response

{
  "count": 7
}

GET/collections/todos/:id

Get a Record

This endpoint will return a single record from the collection matching the id parameter.

Optional attributes

  • Name
    id
    Type
    string
    Description

    The ID of the record to get.

Response

Returns the record matching the id parameter.

Request

curl -L "http://localhost:3000/collections/todos/4"

Response

{
  "id": 4,
  "status": false,
  "message": "The API is working",
}

PUT/collections/todos/:id

Update a Record

This endpoint will update the records from the collection matching the id parameter. You can send the data in the request body. The data will be validated using the schema defined in your Drizzle Schema by generating zod schemas.

Optional attributes

  • Name
    id
    Type
    string
    Description

    The ID of the record to update.

Response

Returns the updated records.

Request

curl -L -X PUT "http://localhost:3000/collections/todos/4" \
-H "Content-Type: application/json" \
-d "{
    \"message\": \"Update is working\"
}"

Response

[
    {
        "id": 4,
        "status": false,
        "message": "Update is working",
    }
]

DELETE/collections/todos/:id

Delete Records

This endpoint will delete all the records matching your queryKey, and return the deleted records. For unique keys, it will delete only one record.

Optional attributes

  • Name
    id
    Type
    string
    Description

    The ID of the record to delete.

Response

Returns an array of the deleted records.

Request

curl -L -X DELETE "http://localhost:3000/collections/todos/4"

Response

[
  {
    "id": 4,
    "status": false,
    "message": "The API is working"
  }
]