https://github.com/phasehq/python-sdk
Python SDK for Phase
https://github.com/phasehq/python-sdk
Last synced: 12 months ago
JSON representation
Python SDK for Phase
- Host: GitHub
- URL: https://github.com/phasehq/python-sdk
- Owner: phasehq
- License: mit
- Created: 2023-04-28T09:39:12.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2025-04-06T10:46:42.000Z (about 1 year ago)
- Last Synced: 2025-05-30T00:05:59.470Z (about 1 year ago)
- Language: Python
- Size: 122 KB
- Stars: 4
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# Python SDK for Phase
SDK to integrate Phase in server-side applications running Python. This SDK allows you to manage secrets securely using the Phase platform.
## Install
```
pip install phase-dev
```
## Import
```python
from phase import Phase, CreateSecretsOptions, GetAllSecretsOptions, GetSecretOptions, UpdateSecretOptions, DeleteSecretOptions
```
## Initialize
Initialize the SDK with your host and token:
```python
phase = Phase(
init=False,
host='https://your-phase-host.com',
pss=PHASE_SERVICE_TOKEN
)
```
## Usage
### Create Secrets
Create one or more secrets in a specified application and environment:
```python
create_options = CreateSecretsOptions(
env_name="Development",
app_name="Your App Name",
key_value_pairs=[
{"API_KEY": "your-api-key"},
{"DB_PASSWORD": "your-db-password"}
],
secret_path="/api"
)
result = phase.create_secrets(create_options)
print(f"Create secrets result: {result}")
```
### Get Secrets
Fetch one or more secrets from a specified application and environment:
```python
get_options = GetAllSecretsOptions(
env_name="Development",
app_name="Your App Name",
tag="api", # Optional: filter by tag
secret_path="/api" # Optional: specify path
)
secrets = phase.get_all_secrets(get_options)
for secret in secrets:
print(f"Key: {secret.key}, Value: {secret.value}")
```
To get a specific secret:
```python
get_options = GetSecretOptions(
env_name="Development",
app_name="Your App Name",
key_to_find="API_KEY",
secret_path="/api"
)
secret = phase.get_secret(get_options)
if secret:
print(f"Key: {secret.key}, Value: {secret.value}")
```
### Update Secrets
Update an existing secret in a specified application and environment:
```python
update_options = UpdateSecretOptions(
env_name="Development",
app_name="Your App Name",
key="API_KEY",
value="new-api-key-value",
secret_path="/api",
destination_path="/new-api", # Optional: move secret to a new path
override=False, # Optional: create a personal override
toggle_override=False # Optional: toggle personal override
)
result = phase.update_secret(update_options)
print(f"Update result: {result}")
```
### Delete Secrets
Delete a secret from a specified application and environment:
```python
delete_options = DeleteSecretOptions(
env_name="Development",
app_name="Your App Name",
key_to_delete="API_KEY",
secret_path="/api"
)
result = phase.delete_secret(delete_options)
print(f"Delete result: {result}")
```
## Error Handling
The SDK methods may raise exceptions for various error conditions. It's recommended to wrap SDK calls in try-except blocks to handle potential errors:
```python
try:
get_options = GetAllSecretsOptions(env_name="Development", app_name="Your App Name")
secrets = phase.get_all_secrets(get_options)
except ValueError as e:
print(f"An error occurred: {e}")
```
## Note on Security
Never hard-code sensitive information like tokens or secrets directly in your code. Always use environment variables or secure configuration management to provide these values to your application.