Welcome to the PocketBase Python SDK! This SDK simplifies the process of interacting with the PocketBase API, allowing you to manage collections, records, and authentication directly through Python.
- Authentication: Securely connect with the PocketBase API using tokens.
- CRUD Operations: Create, read, update, and delete records in specified collections.
- Dynamic Collections: Easily interact with multiple collections.
- Token Generation: Generate tokens using username and password.
- Filtering & Pagination: Optional support for filtering and paginating results.
- User Management: Handle user verification, password resets, and email changes.
- OAuth2 Provider Management: Integrate with various OAuth2 providers.
- Customizable Requests: Tailor your request handling to fit your needs.
Before you begin, ensure you have the following:
- Python 3.8 or higher
- requests library
- A valid PocketBase instance URL
Follow these steps to install the PocketBase Python SDK:
-
Clone the repository or download the source files:
git clone https://github.com/itzreqle/pocketbase-python-sdk.git
-
Install required dependencies using pip:
pip install -r requirements.txt
-
Create a
.env
file in your project root with the following environment variables:POCKETBASE_BASE_URL=https://your-pocketbase-instance.com POCKETBASE_COLLECTION=your_collection_name POCKETBASE_API_TOKEN=your_api_token
To start using the SDK, instantiate the PocketBase
class:
from pocketbase import PocketBase
pb = PocketBase()
Authenticate users with various methods:
# Password authentication
response = pb.auth_with_password('[email protected]', 'password123')
# OAuth2 authentication
response = pb.auth_with_oauth2_flow({'provider': 'google'})
# Refresh authentication
response = pb.refresh_auth()
Manage your records with ease:
# Get all records
query_params = {'filter': 'status=active'}
response = pb.list_records(query_params=query_params, page=1, per_page=20)
print(response)
# Get record by ID
record_id = 'your-record-id'
response = pb.get_record(record_id)
print(response)
# Create a new record
data = {'name': 'New Item', 'description': 'A description of the item.'}
response = pb.create_record(data)
print(response)
# Update a record
record_id = 'your-record-id'
data = {'name': 'Updated Item Name'}
response = pb.update_record(record_id, data)
print(response)
# Delete a record
record_id = 'your-record-id'
response = pb.delete_record(record_id)
print(response)
Manage user accounts seamlessly:
# Request email verification
response = pb.request_verification('[email protected]')
# Confirm email verification
response = pb.confirm_verification('verification_token')
# Request password reset
response = pb.request_password_reset('[email protected]')
# Confirm password reset
response = pb.confirm_password_reset('reset_token', 'new_password', 'new_password_confirm')
Manage authentication tokens easily:
# Generate a new token
username = 'your-username'
password = 'your-password'
response = pb.generate_token(username, password)
print(response)
# Manually set a new token
pb.set_token('your-new-token')
All API responses are returned as dictionaries, including the status code and response data. For example:
{
'status_code': 200,
'data': {
'id': 'abc123',
'name': 'Item Name',
'created': '2023-01-01T12:00:00Z'
}
}
In case of errors, the SDK will return an error message along with the HTTP status code.
We welcome contributions! Feel free to submit a Pull Request to enhance the SDK.
If you encounter any issues or have questions, please open an issue on the GitHub repository.
This project is licensed under the MIT License.