Skip to content

NpgsqlRest Options

NpgsqlRest HTTP middleware general configuration for endpoint generation and request handling.

Overview

json
{
  "NpgsqlRest": {
    "ConnectionName": null,
    "UseMultipleConnections": false,
    "CommandTimeout": null,
    "SchemaSimilarTo": null,
    "SchemaNotSimilarTo": null,
    "IncludeSchemas": null,
    "ExcludeSchemas": null,
    "NameSimilarTo": null,
    "NameNotSimilarTo": null,
    "IncludeNames": null,
    "ExcludeNames": null,
    "CommentsMode": "OnlyWithHttpTag",
    "UrlPathPrefix": "/api",
    "KebabCaseUrls": true,
    "CamelCaseNames": true,
    "RequiresAuthorization": true,
    "LogConnectionNoticeEvents": true,
    "LogConnectionNoticeEventsMode": "FirstStackFrameAndMessage",
    "LogCommands": false,
    "LogCommandParameters": false,
    "DefaultHttpMethod": null,
    "DefaultRequestParamType": null,
    "RequestHeadersMode": "Parameter",
    "RequestHeadersContextKey": "request.headers",
    "RequestHeadersParameterName": "_headers",
    "InstanceIdRequestHeaderName": null,
    "CustomRequestHeaders": {},
    "ExecutionIdHeaderName": "X-NpgsqlRest-ID",
    "DefaultServerSentEventsEventNoticeLevel": "INFO",
    "ServerSentEventsResponseHeaders": {},
    "RoutineOptions": { ... }
  }
}

See Routine Options for RoutineOptions configuration.

Connection Settings

SettingTypeDefaultDescription
ConnectionNamestringnullConnection name from ConnectionStrings section. Uses first available if null.
UseMultipleConnectionsboolfalseAllow individual routines to use different connections from ConnectionStrings.
CommandTimeoutstringnullCommand timeout in PostgreSQL interval format (e.g., "30 seconds", "1 minute"). Uses default 30 seconds if null. Can be overridden per endpoint with command_timeout annotation.

Schema and Name Filtering

Filter which PostgreSQL routines are exposed as endpoints.

SettingTypeDefaultDescription
SchemaSimilarTostringnullInclude schemas matching this SQL SIMILAR TO pattern.
SchemaNotSimilarTostringnullExclude schemas matching this SQL SIMILAR TO pattern.
IncludeSchemasarraynullList of schema names to include.
ExcludeSchemasarraynullList of schema names to exclude.
NameSimilarTostringnullInclude routine names matching this SQL SIMILAR TO pattern.
NameNotSimilarTostringnullExclude routine names matching this SQL SIMILAR TO pattern.
IncludeNamesarraynullList of routine names to include.
ExcludeNamesarraynullList of routine names to exclude.

Filtering Examples

Include only specific schemas:

json
{
  "NpgsqlRest": {
    "IncludeSchemas": ["api", "public"]
  }
}

Exclude internal schemas:

json
{
  "NpgsqlRest": {
    "ExcludeSchemas": ["pg_catalog", "information_schema", "internal"]
  }
}

Filter by name pattern:

json
{
  "NpgsqlRest": {
    "NameSimilarTo": "api_%",
    "NameNotSimilarTo": "%_internal"
  }
}

Comments Mode

SettingTypeDefaultDescription
CommentsModestring"OnlyWithHttpTag"How comment annotations affect endpoint creation.

Available modes:

ModeDescription
IgnoreCreate all endpoints, ignore comment annotations.
ParseAllCreate all endpoints, parse comment annotations to modify them.
OnlyWithHttpTagOnly create endpoints for routines with HTTP tag in comments (default).

URL and Naming

SettingTypeDefaultDescription
UrlPathPrefixstring"/api"URL prefix for all generated endpoints.
KebabCaseUrlsbooltrueConvert URL paths to kebab-case from PostgreSQL names.
CamelCaseNamesbooltrueConvert parameter names to camelCase from PostgreSQL names.

URL Examples

With default settings, get_user_profile becomes /api/get-user-profile.

json
{
  "NpgsqlRest": {
    "UrlPathPrefix": "/v1/api",
    "KebabCaseUrls": true
  }
}

Authorization

SettingTypeDefaultDescription
RequiresAuthorizationbooltrueForce all endpoints to require authorization. Can be overridden per endpoint via comment annotations.

Logging

SettingTypeDefaultDescription
LogConnectionNoticeEventsbooltrueLog PostgreSQL connection events (triggered by RAISE statements).
LogConnectionNoticeEventsModestring"FirstStackFrameAndMessage"How to format notice event logs.
LogCommandsboolfalseLog every executed command and query at debug level.
LogCommandParametersboolfalseInclude parameter values in command logs. Only applies when LogCommands is true.

Notice Event Modes

ModeDescription
MessageOnlyLog only the message.
FirstStackFrameAndMessageLog first stack frame and message (default).
FullStackAndMessageLog full stack trace and message.

HTTP Method and Parameters

SettingTypeDefaultDescription
DefaultHttpMethodstringnullForce HTTP method for all endpoints (GET, POST, PUT, DELETE, etc.).
DefaultRequestParamTypestringnullForce parameter location for all endpoints (QueryString or BodyJson).

Default Behavior

When DefaultHttpMethod is null:

  • GET is used when routine is not volatile, or name starts with get_, contains _get_, or ends with _get
  • POST is used otherwise

When DefaultRequestParamType is null:

  • QueryString for GET and DELETE endpoints
  • BodyJson for all other methods

Request Headers

SettingTypeDefaultDescription
RequestHeadersModestring"Parameter"How to send request headers to PostgreSQL routines.
RequestHeadersContextKeystring"request.headers"Context variable name when mode is Context.
RequestHeadersParameterNamestring"_headers"Parameter name when mode is Parameter.
CustomRequestHeadersobject{}Custom headers added to requests before sending to PostgreSQL.
InstanceIdRequestHeaderNamestringnullHeader name for NpgsqlRest instance ID. Set to null to disable.
ExecutionIdHeaderNamestring"X-NpgsqlRest-ID"Header name for request tracking and SSE correlation.

Request Headers Modes

ModeDescription
IgnoreDon't send request headers to routines.
ContextSet context variable context.headers with JSON string via set_config().
ParameterSend headers to parameter named by RequestHeadersParameterName. Parameter must be JSON/text type with default value.

Server-Sent Events

SettingTypeDefaultDescription
DefaultServerSentEventsEventNoticeLevelstring"INFO"Minimum PostgreSQL notice level for SSE events (INFO, NOTICE, WARNING).
ServerSentEventsResponseHeadersobject{}Custom headers added to SSE responses.

Complete Example

Production configuration:

json
{
  "NpgsqlRest": {
    "ConnectionName": null,
    "UseMultipleConnections": true,
    "CommandTimeout": "30 seconds",
    "IncludeSchemas": ["api"],
    "ExcludeSchemas": ["internal"],
    "CommentsMode": "OnlyWithHttpTag",
    "UrlPathPrefix": "/api",
    "KebabCaseUrls": true,
    "CamelCaseNames": true,
    "RequiresAuthorization": true,
    "LogConnectionNoticeEvents": true,
    "LogConnectionNoticeEventsMode": "FirstStackFrameAndMessage",
    "LogCommands": false,
    "LogCommandParameters": false,
    "RequestHeadersMode": "Parameter",
    "RequestHeadersParameterName": "_headers",
    "ExecutionIdHeaderName": "X-NpgsqlRest-ID"
  }
}

Development configuration with verbose logging:

json
{
  "NpgsqlRest": {
    "CommentsMode": "ParseAll",
    "RequiresAuthorization": false,
    "LogConnectionNoticeEvents": true,
    "LogConnectionNoticeEventsMode": "FullStackAndMessage",
    "LogCommands": true,
    "LogCommandParameters": true
  }
}

Next Steps

Released under the MIT License.