Skip to content

Utilities

APIful not only allows you to build powerful clients, it also provides a set of utilities that you can use in your server-side code. Use them in the web framework of your choice, such as Hono, Elysia, or Nitro, to make your life easier.

HTTP Status Codes

List of all common HTTP status codes as individually exported constants.

HTTP Status Reason Phrases

List of all common HTTP status reason phrases as individually exported constants.

JSON to Type Definition

Create TypeScript interfaces from JSON data using the jsonToTypeDefinition function. Useful for defining API response types or working with dynamic JSON structures.

Generic Schema & Validation

Create and validate JSON schemas using the jsonSchema and validateTypes functions. Ensure type safety at runtime using your defined schemas.

Examples

Nitro

For the Nitro server toolkit, use HTTP status codes and phrases in your event handlers like this:

ts
import * as HttpStatusCodes from 'apiful/http-status-codes'
import * as HttpStatusPhrases from 'apiful/http-status-phrases'

export default defineEventHandler(async (event) => {
  throw createError({
    statusCode: HttpStatusCodes.NOT_FOUND,
    statusMessage: HttpStatusPhrases.NOT_FOUND,
  })
})

Hono

For web frameworks like Hono, use the status codes and phrases in your route handlers like this:

ts
import * as HttpStatusCodes from 'apiful/http-status-codes'
import * as HttpStatusPhrases from 'apiful/http-status-phrases'

app.get('/not-found', (c) => {
  c.status(HttpStatusCodes.NOT_FOUND)
  c.json({ message: HttpStatusPhrases.NOT_FOUND })
})