Python SDK for Hyphen - Feature toggles, IP geolocation, and link shortening
Project description
Hyphen Python SDK
The official Python SDK for Hyphen - providing feature toggles, IP geolocation, and link shortening services.
Installation
pip install hyphen
For development:
pip install hyphen[dev]
Quick Start
Environment Variables
You can set API credentials using environment variables:
export HYPHEN_API_KEY="your_api_key"
export HYPHEN_PUBLIC_API_KEY="your_public_api_key"
export HYPHEN_APPLICATION_ID="your_application_id"
export HYPHEN_ORGANIZATION_ID="your_organization_id"
Feature Toggles
Manage feature flags for your application with targeting support.
Basic Usage
from hyphen import FeatureToggle, ToggleContext
toggle = FeatureToggle(
application_id='your_application_id',
api_key='your_api_key',
environment='production', # Optional, defaults to HYPHEN_ENVIRONMENT or "production"
)
# Get a boolean toggle with default value
enabled = toggle.get_boolean('my-feature', default=False)
print('Feature enabled:', enabled)
Targeting Context
Use targeting context to evaluate toggles based on user attributes:
from hyphen import FeatureToggle, ToggleContext
# Set a default context for all evaluations
toggle = FeatureToggle(
application_id='your_application_id',
api_key='your_api_key',
default_context=ToggleContext(
targeting_key='user_123',
user={'id': 'user_123', 'email': 'user@example.com'},
)
)
# Or pass context per request
context = ToggleContext(
targeting_key='user_456',
ip_address='192.168.1.1',
custom_attributes={'plan': 'premium', 'beta_tester': True}
)
enabled = toggle.get_boolean('premium-feature', default=False, context=context)
Type-Safe Toggle Methods
from hyphen import FeatureToggle
toggle = FeatureToggle(
application_id='your_application_id',
api_key='your_api_key',
)
# Boolean toggles
enabled = toggle.get_boolean('feature-flag', default=False)
# String toggles
theme = toggle.get_string('ui-theme', default='light')
# Numeric toggles
max_items = toggle.get_number('max-items', default=10)
# JSON object toggles
config = toggle.get_object('feature-config', default={'enabled': False})
Get Multiple Toggles
from hyphen import FeatureToggle
toggle = FeatureToggle(
application_id='your_application_id',
api_key='your_api_key',
)
toggles = toggle.get_toggles(['feature-a', 'feature-b', 'feature-c'])
print('Toggles:', toggles) # {'feature-a': True, 'feature-b': 42, 'feature-c': 'enabled'}
Error Handling
from hyphen import FeatureToggle
def handle_toggle_error(error):
print(f'Toggle evaluation failed: {error}')
toggle = FeatureToggle(
application_id='your_application_id',
api_key='your_api_key',
on_error=handle_toggle_error, # Errors call this instead of raising
)
# Returns default value on error instead of raising
enabled = toggle.get_boolean('my-feature', default=False)
Toggles support multiple data types:
- Boolean:
TrueorFalse - Number:
42(int or float) - String:
"Hello World!" - JSON:
{"id": "Hello World!"}
NetInfo - IP Geolocation
Look up IP address geolocation information.
Get Single IP Information
from hyphen import NetInfo
net_info = NetInfo(api_key='your_api_key')
ip_info = net_info.get_ip_info('8.8.8.8')
print('IP Info:', ip_info)
Get Multiple IP Information
from hyphen import NetInfo
net_info = NetInfo(api_key='your_api_key')
ips = ['8.8.8.8', '1.1.1.1']
ip_infos = net_info.get_ip_infos(ips)
print('IP Infos:', ip_infos)
Link - Short Code Service
Create and manage short URLs and QR codes.
Creating a Short Code
from hyphen import Link
link = Link(
organization_id='your_organization_id',
api_key='your_api_key',
)
response = link.create_short_code(
long_url='https://hyphen.ai',
domain='test.h4n.link',
options={
'tags': ['sdk-test', 'unit-test'],
}
)
print('Short Code Response:', response)
Updating a Short Code
from hyphen import Link
link = Link(
organization_id='your_organization_id',
api_key='your_api_key',
)
response = link.update_short_code(
code='code_1234567890',
options={
'title': 'Updated Short Code',
'tags': ['sdk-test', 'unit-test'],
'long_url': 'https://hyphen.ai/updated',
}
)
print('Update Response:', response)
Getting a Short Code
from hyphen import Link
link = Link(
organization_id='your_organization_id',
api_key='your_api_key',
)
response = link.get_short_code('code_1234567890')
print('Short Code:', response)
Getting Short Codes
from hyphen import Link
link = Link(
organization_id='your_organization_id',
api_key='your_api_key',
)
response = link.get_short_codes(
title='My Short Codes',
tags=['sdk-test', 'unit-test']
)
print('Short Codes:', response)
Getting Organization Tags
from hyphen import Link
link = Link(
organization_id='your_organization_id',
api_key='your_api_key',
)
tags = link.get_tags()
print('Tags:', tags)
Get Short Code Stats
from hyphen import Link
from datetime import datetime
link = Link(
organization_id='your_organization_id',
api_key='your_api_key',
)
stats = link.get_short_code_stats(
code='code_1234567890',
start_date=datetime(2023, 1, 1),
end_date=datetime(2023, 12, 31)
)
print('Stats:', stats)
Deleting a Short Code
from hyphen import Link
link = Link(
organization_id='your_organization_id',
api_key='your_api_key',
)
response = link.delete_short_code('code_1234567890')
print('Delete Response:', response)
Creating a QR Code
from hyphen import Link, QrSize
link = Link(
organization_id='your_organization_id',
api_key='your_api_key',
)
response = link.create_qr_code(
code='code_1234567890',
options={
'title': 'My QR Code',
'backgroundColor': '#ffffff',
'color': '#000000',
'size': QrSize.MEDIUM,
}
)
print('QR Code:', response)
Get QR Code by ID
from hyphen import Link
link = Link(
organization_id='your_organization_id',
api_key='your_api_key',
)
response = link.get_qr_code('code_1234567890', 'qr_1234567890')
print('QR Code:', response)
Get QR Codes for a Short Code
from hyphen import Link
link = Link(
organization_id='your_organization_id',
api_key='your_api_key',
)
response = link.get_qr_codes('code_1234567890')
print('QR Codes:', response)
Deleting a QR Code
from hyphen import Link
link = Link(
organization_id='your_organization_id',
api_key='your_api_key',
)
response = link.delete_qr_code('code_1234567890', 'qr_1234567890')
print('Delete Response:', response)
Development
Setup
# Clone the repository
git clone https://github.com/Hyphen/python-sdk.git
cd python-sdk
# Install dependencies
pip install -e ".[dev]"
Testing
Create a .env file with your test credentials:
HYPHEN_PUBLIC_API_KEY=your_public_api_key
HYPHEN_API_KEY=your_api_key
HYPHEN_APPLICATION_ID=your_application_id
HYPHEN_LINK_DOMAIN=your_link_domain
HYPHEN_ORGANIZATION_ID=your_organization_id
Run tests:
pytest
Linting
ruff check hyphen tests
Type Checking
mypy hyphen
Releasing
Releases are published to PyPI automatically when a GitHub Release is created.
To release a new version:
- Update the version in
pyproject.tomlandhyphen/__init__.py - Commit the version change:
git commit -am "chore: bump version to X.Y.Z" - Push to main:
git push origin main - Create a new GitHub Release:
- Tag:
vX.Y.Z(e.g.,v0.1.0) - Title:
vX.Y.Z - Description: Release notes
- Tag:
- The release workflow will automatically run tests and publish to PyPI
Note: Publishing uses PyPI's trusted publisher (OIDC) - no API token needed.
Contributing
We welcome contributions! Please follow these steps:
- Fork the repository
- Create a new branch for your feature or bug fix
- Make your changes and commit them with clear messages:
feat: describe the featurefix: describe the bug fixchore: describe maintenance task
- Run tests and linting to ensure quality
- Push your changes to your forked repository
- Create a pull request to the main repository
License
This project is licensed under the MIT License. See the LICENSE file for details.
Copyright © 2025 Hyphen, Inc. All rights reserved.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file hyphen-0.5.0.tar.gz.
File metadata
- Download URL: hyphen-0.5.0.tar.gz
- Upload date:
- Size: 21.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e4bd90dab8ec003d17cd0904fe52591043b2b81b7235a2b34901bcd235b9fc8d
|
|
| MD5 |
e0494e0f80a10ec18d4ad2402a58e2ec
|
|
| BLAKE2b-256 |
d0d8cd328781a2b3b4bb6b4ed249fe03b79a521e649c179e7190ef17d49c177f
|
Provenance
The following attestation bundles were made for hyphen-0.5.0.tar.gz:
Publisher:
release.yaml on Hyphen/python-sdk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
hyphen-0.5.0.tar.gz -
Subject digest:
e4bd90dab8ec003d17cd0904fe52591043b2b81b7235a2b34901bcd235b9fc8d - Sigstore transparency entry: 855624281
- Sigstore integration time:
-
Permalink:
Hyphen/python-sdk@397d1ad9978cdcbc14bcf30d4a9405f235514cb0 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/Hyphen
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@397d1ad9978cdcbc14bcf30d4a9405f235514cb0 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file hyphen-0.5.0-py3-none-any.whl.
File metadata
- Download URL: hyphen-0.5.0-py3-none-any.whl
- Upload date:
- Size: 14.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7717ec43e3a58e7041dfe566cfed48b25cc0bb6573d756d58d54806b2ead9c19
|
|
| MD5 |
6a2661c28da05101a71ed3038445e28b
|
|
| BLAKE2b-256 |
6be07ed485262a2c490518d53e85aab89689c16786d903a26f2c7f5e1f751582
|
Provenance
The following attestation bundles were made for hyphen-0.5.0-py3-none-any.whl:
Publisher:
release.yaml on Hyphen/python-sdk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
hyphen-0.5.0-py3-none-any.whl -
Subject digest:
7717ec43e3a58e7041dfe566cfed48b25cc0bb6573d756d58d54806b2ead9c19 - Sigstore transparency entry: 855624309
- Sigstore integration time:
-
Permalink:
Hyphen/python-sdk@397d1ad9978cdcbc14bcf30d4a9405f235514cb0 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/Hyphen
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@397d1ad9978cdcbc14bcf30d4a9405f235514cb0 -
Trigger Event:
workflow_dispatch
-
Statement type: