Skip to content
Written with Claude
IMPORTANT

As you may notice, this page and pretty much the entire website were obviously created with the help of AI. I wonder how you could tell? Was it a big "Written With Claude" badge on every page? I moved it to the top now (with the help of AI of course) to make it even more obvious. There are a few blogposts that were written by me manually, the old-fashioned way, I hope there will be more in the future, and those have a similar "Human Written" badge. This project (not the website), on the other hand, is a very, very different story. It took me more than two years of painstaking and unpaid work in my own free time. A story that, hopefully, I will tell someday. But meanwhile, what would you like me to do? To create a complex documentation website with a bunch of highly technical articles with the help of AI and fake it, to give you an illusion that I also did that manually? Like the half of itnernet is doing at this point? How does that makes any sense? Is that even fair to you? Or maybe to create this website manually, the old-fashioned way, just for you? While working a paid job for a salary, most of you wouldn't even get up in the morning. Would you like me to sing you a song while we're at it? For your personal entertainment? Seriously, get a grip. Do you find this information less valuable because of the way this website was created? I give my best to fix it to keep the information as accurate as possible, and I think it is very accurate at this point. If you find some mistakes, inaccurancies or problems, there is a comment section at the bottom of every page, which I also made with the help of the AI. And I woould very much appreciate if you leave your feedback there. Look, I'm just a guy who likes SQL, that's all. If you don't approve of how this website was constructed and the use of AI tools, I suggest closing this page and never wever coming back. And good riddance. And I would ban your access if I could know how. Thank you for your attention to this matter.

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",
    "QueryStringNullHandling": "Ignore",
    "TextResponseNullHandling": "EmptyString",
    "DefaultServerSentEventsEventNoticeLevel": "INFO",
    "ServerSentEventsResponseHeaders": {},
    "RoutineOptions": { ... },
    "AuthenticationOptions": { ... },
    "CrudSource": { ... },
    "UploadOptions": { ... },
    "ClientCodeGen": { ... },
    "HttpFileOptions": { ... },
    "OpenApiOptions": { ... }
  }
}

See related configuration pages:

Connection Settings

SettingTypeDefaultDescription
ConnectionNamestringnullConnection name from ConnectionStrings section. Uses first available if null.
UseMultipleConnectionsboolfalseAllow individual routines to use different connections from ConnectionStrings.
CommandTimeoutstringnullCommand timeout using interval format (e.g., "30s", "1m"). 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 annotation in comments (default).

With the default OnlyWithHttpTag mode, routines without the HTTP annotation in their comment will not be exposed as endpoints. This provides explicit control over which database routines are accessible via the API.

See also CRUD Source CommentsMode for table/view endpoint generation settings.

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"Execution request header name. Used for request tracking and SSE correlation and in ConnectionSettings.UseJsonApplicationName.

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.

NULL Handling

SettingTypeDefaultDescription
QueryStringNullHandlingstring"Ignore"How empty or "null" query string values are interpreted.
TextResponseNullHandlingstring"EmptyString"How NULL database results are returned in plain text responses.

QueryStringNullHandling Values

ValueDescription
IgnoreNo special handling - empty strings stay as empty strings, "null" literal stays as "null" string (default).
EmptyStringEmpty query string values are interpreted as NULL values.
NullLiteralLiteral string "null" (case insensitive) is interpreted as NULL value.

TextResponseNullHandling Values

ValueDescription
EmptyStringReturns an empty string response with status code 200 OK (default).
NullLiteralReturns a string literal "NULL" with status code 200 OK.
NoContentReturns status code 204 NO CONTENT.

These settings can be overridden per-endpoint using comment annotations:

sql
comment on function my_func(text) is '
query_string_null_handling empty_string
text_response_null_handling no_content
';

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

Comments

Released under the MIT License.