NpgsqlRest 2.30.0

dotnet add package NpgsqlRest --version 2.30.0
                    
NuGet\Install-Package NpgsqlRest -Version 2.30.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NpgsqlRest" Version="2.30.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NpgsqlRest" Version="2.30.0" />
                    
Directory.Packages.props
<PackageReference Include="NpgsqlRest" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add NpgsqlRest --version 2.30.0
                    
#r "nuget: NpgsqlRest, 2.30.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package NpgsqlRest@2.30.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=NpgsqlRest&version=2.30.0
                    
Install as a Cake Addin
#tool nuget:?package=NpgsqlRest&version=2.30.0
                    
Install as a Cake Tool

NpgsqlRest

Build, Test, Publish and Release License GitHub Stars GitHub Forks

Automatic REST API for PostgreSQL Databases implemented as AOT-Ready .NET8/.NET9 Middleware

If you have a PostgreSQL database - NpgsqlRest can create blazing fast REST API automatically and write client code for your project.

Features:

  • Nuget package for .NET that creates discoverable REST API automatically from PostgreSQL database.
  • High Performance. See Performances Benchmarks.
  • Modular Design with a Plug-in System. Create API for functions and procedure, create CRUD endpoints for your tables, create HTTP files, and Typescript client code.
  • AOT-Ready. Ahead-of-time compiled to the native code. No dependencies, native executable, it just runs and it's very fast.
  • Customizable. Configure endpoints with powerful comment annotations. You can easily configure any endpoint by adding comment annotation labels to PostgreSQL Comments.
  • Real-time Streaming. Server-sent events support with PostgreSQL RAISE INFO statements for live notifications.
  • Advanced Authentication. Role-based authorization with flexible scope control.
  • Standalone Executable Web Client. Download the executable and run it. No installation required. See Releases.

Standalone Client Application

The standalone client provides a production-ready REST API server with extensive configuration options:

  • Authentication: Cookie auth, Bearer tokens, OAuth (Google, LinkedIn, GitHub, Microsoft, Facebook)
  • Security: SSL/TLS, CORS, antiforgery tokens, data protection with configurable encryption
  • File Operations: Static file serving with template parsing, file uploads (filesystem, PostgreSQL Large Objects, CSV/Excel processing)
  • Performance: Response compression, configurable Kestrel limits, thread pool tuning
  • Monitoring: Comprehensive logging (console, file, PostgreSQL), request tracking, connection analytics
  • Code Generation: Auto-generated HTTP files and TypeScript/JavaScript clients
  • CRUD Operations: Automatic table/view endpoints with customizable URL patterns

Simply download, configure via JSON, and deploy - no .NET installation required.

Quick Example

1) Your PostgreSQL Function
create function hello_world()                                    
returns text 
language sql
as $$
select 'Hello World'
$$;
2) .NET8 AOT-Ready Web App
var builder = WebApplication.CreateSlimBuilder(args);
var app = builder.Build();
var connectionStr = "Host=localhost;Port=5432;Database=my_db;Username=postgres;Password=postgres";
app.UseNpgsqlRest(new(connectionStr));
app.Run();
3) Auto-Generated HTTP File
@host=http://localhost:5000                                      

// function public.hello_world()
// returns text
POST {{host}}/api/hello-world/
4) Auto-Generated Typescript Client Module
const _baseUrl = "http://localhost:5000";                        


/**
* function public.get_latest_customer()
* returns customers
* 
* @remarks
* GET /api/get-latest-customer
* 
* @see FUNCTION public.get_latest_customer
*/
export async function getHelloWorld() : Promise<string> {
    const response = await fetch(_baseUrl + "/api/hello-world", {
        method: "GET",
        headers: { "Content-Type": "application/json" },
    });
    return await response.text() as string;
}
5) Endpoint Response
HTTP/1.1 200 OK                                                  
Connection: close
Content-Type: text/plain
Date: Tue, 09 Jan 2024 14:25:26 GMT
Server: Kestrel
Transfer-Encoding: chunked

Hello World

Comment Annotations

Configure individual endpoints with powerful and simple routine comment annotations. You can use any PostgreSQL administration tool or a simple script to customize HTTP methods, paths, content types, authentication, real-time streaming, and client code generation.

Quick Reference
Annotation Example Purpose
HTTP GET /path Custom endpoint path and method
Content-Type: text/html Response content type
authorize role1, role2 Role-based authorization
info_path /events Enable event streaming
tsclient = false Disable TypeScript client generation
Basic Examples

Custom HTTP Method and Path:

create function hello_world_html()                               
language sql 
as 
$$
select '<div>Hello World</div>';
$$

comment on function hello_world_html() is '
HTTP GET /hello
Content-Type: text/html';

Authentication and Authorization:

create function secure_data()
returns json
language sql
as $$
select '{"message": "Secret data"}'::json;
$$;

comment on function secure_data() is '
HTTP GET /api/secure
authorize admin, manager';

Real-time Event Streaming:

create function live_updates()
returns void
language plpgsql
as $$
begin
    raise info 'Processing started...';
    perform pg_sleep(2);
    raise info 'Step 1 completed';
    perform pg_sleep(2);
    raise info 'All done!';
end;
$$;

comment on function live_updates() is '
HTTP POST /api/live-updates
info_path /events
info_scope all';

TypeScript Client Control:

create function admin_function()
returns text
language sql
as $$
select 'Admin data';
$$;

comment on function admin_function() is '
HTTP GET /admin/data
authorize admin
tsclient_events = true
tsclient_status_code = true';
Parameter Format

You can also use the parameter format for complex configurations:

comment on function my_function() is '
method = GET
path = /custom/endpoint
content_type = application/json
authorize = admin, user
info_path = /stream
info_scope = matching
tsclient = true
tsclient_events = false';
Advanced Info Streaming

Control message scope per individual RAISE INFO statement:

create function detailed_process()
returns void
language plpgsql
as $$
begin
    raise info 'Starting process...' using hint = 'all';
    raise info 'Processing user data...' using hint = 'authorize admin';
    raise info 'Process completed' using hint = 'self';
end;
$$;

Response will have content type text/html:

Connection: close                                                
Content-Type: text/html
Date: Thu, 18 Jan 2024 11:00:39 GMT
Server: Kestrel
Transfer-Encoding: chunked

<div>Hello World</div>

Getting Started

Using Library

Library Installation

Install the package from NuGet by using any of the available methods:

dotnet add package NpgsqlRest --version 2.30.0
NuGet\Install-Package NpgsqlRest -version 2.30.0
<PackageReference Include="NpgsqlRest" Version="2.30.0" />
Library First Use

Your application builder code:

var app = builder.Build();
app.UseNpgsqlRest(new("Host=localhost;Port=5432;Database=my_db;Username=postgres;Password=postgres"));
app.Run();

For all available build options, see the options documentation.

Library Dependencies
  • net9.0
  • Microsoft.NET.Sdk.Web 9.0
  • Npgsql 8.0.5
  • PostgreSQL >= 13

Note: PostgreSQL 13 minimal version is required to run the initial query to get the list of functions. The source code of this query can be found here. For versions prior to version 13, this query can be replaced with a custom query that can run on older versions.

Contributing

Contributions from the community are welcomed. Please make a pull request with a description if you wish to contribute.

License

This project is licensed under the terms of the MIT license.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on NpgsqlRest:

Package Downloads
NpgsqlRest.TsClient

Automatic Typescript Client Code Generation for NpgsqlRest

NpgsqlRest.CrudSource

CRUD Source for NpgsqlRest

NpgsqlRest.HttpFiles

Automatic HTTP Files Generation for NpgsqlRest

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.30.0 59 8/9/2025
2.29.0 143 7/9/2025
2.28.0 250 6/13/2025
2.27.0 156 5/19/2025
2.26.0 128 5/11/2025
2.25.0 146 5/6/2025
2.24.0 158 4/28/2025
2.23.0 148 4/27/2025
2.22.0 506 4/7/2025
2.21.0 462 3/24/2025
2.20.0 231 3/5/2025
2.19.0 111 2/24/2025
2.18.0 109 2/23/2025
2.17.0 120 1/9/2025
2.16.1 114 1/6/2025
2.16.0 111 12/30/2024
2.15.0 132 12/21/2024
2.14.0 149 11/25/2024
2.13.1 130 11/23/2024
2.13.0 143 11/17/2024
2.12.1 144 11/6/2024
2.12.0 112 10/30/2024
2.11.0 154 9/3/2024
2.10.0 157 8/23/2024
2.9.0 91 8/2/2024
2.8.5 136 6/25/2024
2.8.4 135 6/22/2024
2.8.3 132 6/11/2024
2.8.2 134 6/9/2024
2.8.1 111 5/10/2024
2.8.0 122 5/2/2024
2.7.1 136 4/30/2024
2.7.0 151 4/17/2024
2.6.1 146 4/16/2024
2.6.0 139 4/16/2024
2.5.0 143 4/15/2024
2.4.2 158 4/14/2024
2.4.1 134 4/12/2024
2.4.0 207 4/8/2024
2.3.1 135 4/5/2024
2.3.0 166 4/4/2024
2.2.0 131 4/2/2024
2.1.0 147 3/29/2024
2.0.0 161 3/26/2024
1.6.3 146 2/19/2024
1.6.2 149 2/3/2024
1.6.1 134 2/2/2024
1.6.0 133 1/28/2024
1.5.1 121 1/27/2024
1.5.0 130 1/27/2024
1.4.0 127 1/26/2024
1.3.0 134 1/23/2024
1.2.0 138 1/22/2024
1.1.0 140 1/19/2024
1.0.0 147 1/18/2024
0.0.9 118 1/18/2024