OpenAPI Server
Host an OpenAPI 3 REST API directly inside a Delphi HTTP server. Spec-first or code-first, with automatic Swagger UI, request validation and CORS — the spec becomes the single source of truth.
Host an OpenAPI 3 REST API directly inside a Delphi HTTP server. Spec-first or code-first, with automatic Swagger UI, request validation and CORS — the spec becomes the single source of truth.
A drop-in API server that plugs into TsgcWebSocketHTTPServer, loads an OpenAPI 3 spec (or generates one from annotated Pascal classes) and dispatches incoming requests by operationId.
TsgcWSAPIServer_OpenAPI
Windows, macOS, Linux, iOS, Android
Enterprise
Create the component, load (or generate) a spec, attach it to an HTTP server. The component matches routes, validates inputs and serves Swagger UI at /docs automatically.
uses
sgcWebSocket, sgcWebSocket_Server_API_OpenAPI,
sgcHTTP_OpenAPI_Server;
var
Server: TsgcWebSocketHTTPServer;
OpenAPI: TsgcWSAPIServer_OpenAPI;
begin
Server := TsgcWebSocketHTTPServer.Create(nil);
Server.Port := 8080;
OpenAPI := TsgcWSAPIServer_OpenAPI.Create(nil);
OpenAPI.OnRequest := HandleRequest;
OpenAPI.OpenAPIOptions.Endpoint.ServeSwaggerUI := True;
OpenAPI.OpenAPIOptions.Validation.ValidateRequest := True;
OpenAPI.OpenAPIOptions.CORS.Enabled := True;
OpenAPI.LoadFromFile('petstore.json');
OpenAPI.Server := Server;
Server.Active := True;
// Swagger UI: http://localhost:8080/docs
// Raw spec: http://localhost:8080/openapi.json
end;
procedure THost.HandleRequest(Sender: TObject;
const aOperationId: string;
const aContext: TsgcOpenAPIServerContext;
var Handled: Boolean);
begin
Handled := True;
if aOperationId = 'getPetById' then
aContext.RespondJSON(200, PetAsJSON(aContext.PathParamAsInteger('petId')))
else
Handled := False;
end;
uses
sgcWebSocket_Server_API_OpenAPI,
sgcHTTP_OpenAPI_Server_CodeFirst;
type
[sgcServiceContract('Task Manager', '', '1.0.0')]
[sgcRoute('/api/v1')]
TTaskService = class
public
[sgcHttpGet] [sgcRoute('/tasks/{taskId}')]
[sgcSummary('Get a task by ID')]
[sgcResponse(200, 'The requested task')]
[sgcResponse(404, 'Task not found')]
procedure GetTask([sgcFromPath][sgcRequired] const taskId: Integer); virtual;
end;
// Generate the spec from RTTI and feed it to the component:
var
Scanner: TsgcOpenAPICodeFirstScanner;
begin
Scanner := TsgcOpenAPICodeFirstScanner.Create;
try
OpenAPI.LoadFromString(Scanner.GenerateSpec(TTaskService));
finally
Scanner.Free;
end;
OpenAPI.Server := Server;
Server.Active := True;
end;
Two ways to define an API, four pipeline hooks, validation against the spec and a Swagger UI page served by the component itself.
Load an existing OpenAPI 3 document with LoadFromFile or LoadFromString. Routes, parameters and schemas are parsed and become the contract your server must honour — change the JSON, restart, done.
Annotate Pascal classes with sgcHttpGet / sgcRoute / sgcFromPath / sgcResponse attributes; TsgcOpenAPICodeFirstScanner walks the RTTI and emits the spec at runtime — no JSON to hand-write.
Toggle OpenAPIOptions.Endpoint.ServeSwaggerUI and the server publishes an interactive try-it-out page at /docs, fed by /openapi.json — no separate docs build, no static export.
Validates the body, query string, path parameters and required fields against the schemas in the spec. On failure, OnValidationError gives you the full error list and a Continue flag to reject or proceed.
Set OpenAPIOptions.CORS.Enabled := True and the server answers preflight OPTIONS on every route, applying your AllowOrigins / AllowHeaders / AllowMethods policy.
TsgcOpenAPIServerContext exposes PathParamAs*, QueryParamAs*, HeaderValue, BodyAsString / BodyAsJSON plus RespondJSON and an RFC 7807-style RespondError.
OnBeforeRequest, OnAfterRequest, OnAuthenticate and OnException wrap the main OnRequest handler — short-circuit, log, authenticate or remap exceptions without subclassing.
OpenAPIOptions.Endpoint.BasePath prepends a prefix to every route and to the built-in /openapi.json & /docs endpoints, so the API can live behind /api/v1 or any namespace you like.
sgcRequired, sgcMinLength, sgcMaxLength, sgcRange and sgcPattern emit the matching schema constraints so the same rules show up in the generated spec and are enforced at runtime.
Authoritative sources for the protocols and formats this component implements.
Deep-link to the component reference, grab the ready-to-run demo projects, and download the trial.
| Online Help — OpenAPI Server Full property, method and event reference for this component. | Open | |
| Demo Projects — Demos\23.OpenAPI Two ready-to-run examples: spec-first (Petstore JSON) and code-first (task manager). Ship inside the sgcWebSockets package — download the trial below. | Open | |
| User Manual (PDF) Comprehensive manual covering every component in the library. | Open |