Skip to main content

Configuration

Serve

at app.serve() method you can configure the core server parameters if you want to override default notFoundHandler and errorHandler here is the place

export type APIConfig = {
/**
* Application mode: 'development', 'production', 'test', 'staging'
* @env inferance: 'NODE_ENV' or 'APP_MODE' (if both detected app will use 'APP_MODE')
*/
mode: AppMode;
/**
* Openapi application version & version tag for your app
* @env inferance: 'APP_VERSION'
* @default '1.0.0'
*/
version: string;
/**
* Http Port
* @env inferance: 'PORT'
* @default 8080
*/
port: number;
/**
* Enable or disable openapi
* @env inferance: 'OPENAPI'
* @default true
*/
withOpenapi: boolean;
/**
* Set locale
* @warn v 0.x.x only set zod locale at boot time the internal app errors are in english
* @env inferance: 'LOCALE'
* @default 'en'
*/
locale: AppLocale;
/**
* Openapi application title
* @env inferance: 'OPENAPI_TITLE'
* @default 'API'
*/
openapiTitle: string;
/**
* Openapi application description
* @env inferance: 'OPENAPI_DESCRIPTION'
* @default 'API Documentation'
*/
openapiDescription: string;
/**
* Openapi base path
* @env inferance: 'OPENAPI_PATH'
* @default '/reference'
*/
openapiBasePath: string;
/**
* Openapi web UI
* @env inferance: 'OPENAPI_UI'
* @default 'scalar'
*/
openapiUi: OpenApiUi;
/**
* Handler for unmatched routes (404).
*
* Default:
* ```ts
* ({ setStatus, setHeader }) => {
* setStatus(404);
* setHeader('Content-Type', 'text/plain');
* return '';
* }
* ```
*/
notFoundHandler: NotFoundHandler;
/**
* Router exception handler.
*
* Default:
* ```ts
* ({ setStatus, exception }) => {
* setStatus(exception.status);
* return exception.toObject();
* },
* ```
*/
errorHandler: ErrorHandler;
};
app.serve({
notFoundHandler: (req) => {
// req = Request
return new Response(null, { status: 204 });
},
errorHandler: (ctx) => {
// ctx = ErrorContext
return "Upps!";
},
});

Environment variables

Configuration can be supplied via environment variables or programmatically. The following variables are supported:

VariableDescriptionDefault
APP_MODE/NODE_ENVApplication mode (development, production, qa, staging)development
APP_VERSIONAPI/app version1.0.0
PORTHTTP port8080
OPENAPIEnable/disable OpenAPI generation (true/false)true
LOCALEZod error locale (en, es, pt)en
OPENAPI_TITLEGlobal OpenAPI titleAPI
OPENAPI_DESCRIPTIONGlobal OpenAPI descriptionAPI Documentation
OPENAPI_PATHBase path for OpenAPI routes/reference
OPENAPI_UIUI for docs (swagger or scalar)scalar

Example .env:

  • bun create crumbjs creates this .env by default
APP_MODE=development
APP_VERSION=1.0.0
PORT=8080
LOCALE=en
OPENAPI=true
OPENAPI_TITLE=API
OPENAPI_DESCRIPTION=API Documentation
OPENAPI_PATH=/reference
OPENAPI_UI=scalar