# pg-api Examples
This directory contains example code demonstrating how to use the pg-api PostgreSQL REST API driver.
## Examples
### Rust Examples
- **basic_query.rs** - Simple SELECT query with parameters
- **batch_operations.rs** - Execute multiple queries in a single request
- **transaction.rs** - Execute queries within a transaction
- **schema_introspection.rs** - List databases, tables, and inspect schema
### Python Example
- **python_client.py** - Complete Python client library with usage examples
## Running the Examples
### Prerequisites
1. Start the pg-api server:
```bash
cargo run --release
```
2. Configure your API token in the examples (replace `sk_live_your_token_here`)
### Running Rust Examples
```bash
# Install dependencies
cargo add reqwest tokio serde_json
# Run an example
cargo run --example basic_query
cargo run --example batch_operations
cargo run --example transaction
cargo run --example schema_introspection
```
### Running Python Example
```bash
# Install dependencies
pip install requests
# Run the example
python examples/python_client.py
```
## Configuration
All examples expect:
- pg-api server running on `http://localhost:8580`
- Valid API token configured in your accounts.json
- PostgreSQL database accessible with proper permissions
## Creating Your Own Client
The examples demonstrate the basic patterns for interacting with pg-api:
1. **Authentication**: Include API token in Authorization header
2. **Request Format**: Send JSON payload with query, params, and database
3. **Response Handling**: Parse JSON response containing rows and metadata
4. **Error Handling**: Check HTTP status codes and handle errors appropriately
## API Endpoints
- `POST /v1/query` - Execute single query
- `POST /v1/batch` - Execute multiple queries
- `POST /v1/transaction` - Execute queries in transaction
- `GET /v1/databases` - List databases
- `GET /v1/databases/{db}/tables` - List tables
- `GET /v1/databases/{db}/schema` - Get schema
- `GET /health` - Health check