Skip to content

HTTP

Expose a PostgreSQL function or table as an HTTP endpoint.

Keywords

http

Syntax

HTTP
HTTP <method>
HTTP <path>
HTTP <method> <path>

method: GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS

path: Custom URL path (must start with / or be a relative path)

Default Behavior

When method is not specified:

  • GET for non-volatile functions, or names starting with get_, containing _get_, or ending with _get
  • POST for all other functions

When path is not specified, it's generated from the function name using the configured URL prefix and naming conventions.

Examples

Basic Endpoint

sql
create function get_status()
returns text
language sql
as $$select 'OK'$$;

comment on function get_status() is 'HTTP';

Creates: GET /api/get-status

Explicit HTTP Method

sql
create function create_user(_name text)
returns int
language sql
as $$insert into users(name) values(_name) returning id$$;

comment on function create_user(text) is 'HTTP POST';

Creates: POST /api/create-user

Custom Path

sql
create function get_all_users()
returns setof users
language sql
as $$select * from users$$;

comment on function get_all_users() is 'HTTP GET /users';

Creates: GET /users

Method and Custom Path

sql
create function search_products(_query text)
returns setof products
language sql
as $$select * from products where name ilike '%' || _query || '%'$$;

comment on function search_products(text) is 'HTTP GET /products/search';

Creates: GET /products/search

Multi-line with Documentation

sql
comment on function get_user_profile(int) is
'Returns the complete user profile including preferences.
Used by the frontend dashboard.

HTTP GET /users/profile';

The documentation text is ignored; only the HTTP line is parsed.

Unrecognized Method Becomes Path

sql
comment on function my_endpoint() is 'HTTP custom-endpoint';

Since custom-endpoint is not a valid HTTP method, it's treated as a path:

Creates: POST /custom-endpoint

Released under the MIT License.