Hello World Example
Let's create a simple API endpoint associated with a user.
1. Create User and Function
In your database (my_database):
-- 1. Create a user who will log in to the API
CREATE ROLE demo_user WITH LOGIN PASSWORD 'user_password';
GRANT USAGE ON SCHEMA api TO demo_user;
-- 2. Create the Hello World function
-- Input: empty jsonb, Output: json
CREATE OR REPLACE FUNCTION api.hello_world(payload jsonb)
RETURNS json
LANGUAGE sql
AS $$
SELECT '"Hello World"'::json;
$$;
-- 3. Grant permission to the user
GRANT EXECUTE ON FUNCTION api.hello_world(jsonb) TO demo_user;
-- 4. Allow PgArachne (system user) to switch to demo_user
GRANT demo_user TO pgarachne;2. Login via API
Use the JSON-RPC method login to obtain a JWT token:
curl -X POST http://localhost:8080/api/my_database \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"login","params":{"login":"demo_user","password":"user_password"},"id":1}'Response:
{"jsonrpc":"2.0","result":{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."},"id":1}3. Call the Function
Use the token to call the hello_world function:
export TOKEN="YOUR_JWT_TOKEN_HERE"
curl -X POST http://localhost:8080/api/my_database \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "method": "api.hello_world", "params": {}, "id": 1}'Response:
{"jsonrpc": "2.0", "result": "Hello World", "id": 1}