Pascal remains a versatile language, far beyond its traditional desktop roots. With the Horse HTTP framework and CrabPascal v2.22.0, developers can now spin up REST servers straight from .dpr files—no Apache, IIS, or additional dependencies required. A single command like crab-pascal run launches the server, and endpoints become instantly testable via curl.
Modern HTTP development in Pascal
Horse provides a lightweight foundation for REST APIs, offering essential features like route handling, JSON parsing, and middleware-style processing. CrabPascal bundles these capabilities with Delphi-compatible runtime libraries and an integrated HTTP stack, ensuring examples run without external compilers such as GCC. This setup allows developers to focus on API logic rather than infrastructure.
The framework includes ready-to-use examples covering common use cases:
- CRUD operations (
examples/crud/, port 9000) - Time/date ping services (
examples/time-server/, port 9001) - Contact registry systems (
examples/agenda/, port 9000)
Each example can be executed directly via the CrabPascal runtime, streamlining the development workflow.
Crafting a minimal REST endpoint
Creating a simple REST API in Pascal requires minimal code. The following example demonstrates a basic /ping endpoint that returns a JSON response:
program SimpleAPI;
uses
Horse,
System.JSON;
begin
THorse.Get('/ping',
procedure(Req: THorseRequest; Res: THorseResponse; Next: TNextProc)
var
J: TJSONObject;
begin
J := TJSONObject.Create;
J.AddPair('message', 'pong');
Res.Send(J.ToJSON);
J.Free;
end
);
THorse.Listen(9000);
end.To test the API:
crab-pascal run SimpleAPI.dpr
curl The endpoint responds with {"message":"pong"}, confirming successful server operation.
Building a production-ready CRUD API
The examples/crud/crud.dpr project showcases a full CRUD implementation for a product catalog. The code leverages service layers to handle business logic, exposing endpoints for listing, creating, and retrieving products:
THorse.Get('/produtos',
procedure(Req: THorseRequest; Res: THorseResponse; Next: TNextProc)
begin
Res.Send<TJSONObject>(TProdutoService.ListarProdutos);
end
);
THorse.Post('/produtos',
procedure(Req: THorseRequest; Res: THorseResponse; Next: TNextProc)
var
json: TJSONObject;
begin
json := Req.Body<TJSONObject>;
Res.Send(TProdutoService.CriarProduto(
json.GetValue('nome').Value,
json.GetValue('categoria').Value,
StrToFloatDef(json.GetValue('preco').Value, 0),
StrToIntDef(json.GetValue('estoque').Value, 0)
));
end
);Launch the server with:
cd examples/crud
crab-pascal run crud.dprTesting APIs with curl and Postman
The CrabPascal examples include practical test scenarios using curl. Developers can validate endpoints such as:
- List all products: `curl
- Create a product:
curl -X POST \
-H "Content-Type: application/json" \
-d '{"nome":"Notebook","categoria":"Eletrônicos","preco":3499.90,"estoque":10}'- Retrieve a product by ID: `curl
For teams using Postman, the examples can be imported as raw curl commands or directly via a provided Postman collection JSON. This makes it easy to set up regression tests and share API specifications across teams.
Configuration and deployment tips
Adjusting server behavior is straightforward with the crabpascal.toml configuration file. Developers can set the default HTTP port to avoid conflicts when running multiple instances:
[runtime]
default_http_port = 9000This flexibility ensures smooth local development and testing, especially in microservices architectures.
Knowing the limitations
While Horse and CrabPascal provide solid support for core REST functionality—including routing and JSON handling—they do not yet support every Delphi middleware plugin. New HTTP features are released incrementally, as documented in sprint notes. For developers learning REST fundamentals or prototyping backends in Pascal, the included examples offer a complete and functional starting point.
Pascal, combined with Horse and CrabPascal, delivers a compelling backend solution for Delphi developers. Powered by a single Rust-based binary, it enables the creation of efficient, maintainable APIs without leaving the Pascal ecosystem. Start with the examples/crud/ project, experiment with the endpoints, and build your next API with confidence.
AI summary
Pascal dilinde REST API geliştirmeye meraklı mısınız? Horse framework ve CrabPascal ile sıfırdan API oluşturarak backend geliştirmenin inceliklerini keşfedin.