API Reference - Integrations¶
Pydantic¶
instapay_eg.integrations.pydantic
¶
Pydantic v2 integration for instapay-eg.
Provides annotated types and a ready-made model that integrate seamlessly with Pydantic v2 and FastAPI.
Usage::
from pydantic import BaseModel
from instapay_eg.integrations.pydantic import InstaPayLink, InstaPayHandle
class PaymentRequest(BaseModel):
link: InstaPayLink # validates + extracts the URL automatically
recipient: InstaPayHandle # validates the handle format
FastAPI example::
from fastapi import FastAPI
from instapay_eg.integrations.pydantic import InstaPayPaymentModel
app = FastAPI()
@app.post("/payments")
async def create_payment(payment: InstaPayPaymentModel):
return {"link": payment.link, "handle": payment.handle}
Raises:
| Type | Description |
|---|---|
ImportError
|
If |
InstaPayLink
module-attribute
¶
InstaPayLink = Annotated[
str,
AfterValidator(_validate_instapay_link),
Field(
examples=["https://ipn.eg/S/alice/instapay/2DcFGv"],
description="An Egyptian InstaPay payment link. Accepts raw share-sheet text from the 'InstaPay Egypt' app (the link will be extracted and security-checked automatically).",
json_schema_extra={
"format": "uri",
"pattern": "^https://ipn\\.eg/",
},
),
]
Annotated str type that validates, extracts, and security-checks an
InstaPay payment link. Use this as a field type in any Pydantic BaseModel
or FastAPI request/response schema.
InstaPayHandle
module-attribute
¶
InstaPayHandle = Annotated[
str,
AfterValidator(_validate_instapay_handle),
Field(
examples=["alice", "alice@instapay"],
description="An InstaPay handle. The ``@instapay`` suffix is stripped and normalised automatically.",
json_schema_extra={
"pattern": "^[a-zA-Z0-9]+([._\\-][a-zA-Z0-9]+)*$"
},
),
]
Annotated str type that validates and normalises an InstaPay handle.
Accepts both 'alice' and 'alice@instapay' - the suffix is stripped.
InstaPayPaymentModel
¶
Bases: BaseModel
A ready-to-use Pydantic model representing an InstaPay payment request.
Attributes:
| Name | Type | Description |
|---|---|---|
link |
InstaPayLink
|
The validated InstaPay payment URL. |
handle |
InstaPayHandle
|
The normalised recipient handle (without |
Example::
class MyPaymentForm(InstaPayPaymentModel):
note: str = ""
form = MyPaymentForm(
link="https://ipn.eg/S/alice/instapay/2DcFGv",
handle="alice",
)
Source code in src/instapay_eg/integrations/pydantic.py
QR Code¶
instapay_eg.integrations.qrcode
¶
QR code generation integration for instapay-eg.
Generates QR codes for InstaPay payment links using the segno library.
Supports PNG, SVG, and base64-encoded output - covering every common use case
from saving to disk, embedding in web pages, and returning from JSON APIs.
Usage::
from instapay_eg.integrations.qrcode import qr_as_base64, save_qr
# For a JSON API response:
b64 = qr_as_base64("https://ipn.eg/S/alice/instapay/2DcFGv")
# For saving to disk:
save_qr("https://ipn.eg/S/alice/instapay/2DcFGv", "alice_payment.png")
Raises:
| Type | Description |
|---|---|
ImportError
|
If |
generate_qr
¶
Generate a segno.QRCode object for link.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
link
|
str
|
A validated |
required |
error
|
str
|
QR error correction level. One of |
'm'
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
QRCode
|
class: |
QRCode
|
or any other segno method on it directly. |
Example
qr = generate_qr("https://ipn.eg/S/alice/instapay/2DcFGv") qr.save("payment.png", scale=10)
Source code in src/instapay_eg/integrations/qrcode.py
save_qr
¶
save_qr(
link: str,
path: str | Path,
*,
scale: int = 10,
dark: str = "#1a1a2e",
light: str = "#ffffff",
file_format: str | None = None,
) -> None
Save a QR code image for link to path on disk.
The output format is inferred from the file extension of path (e.g.
payment.png → PNG, payment.svg → SVG) unless file_format is
given explicitly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
link
|
str
|
A validated |
required |
path
|
str | Path
|
Destination file path. Parent directories must already exist. |
required |
scale
|
int
|
Pixel size of each QR module. |
10
|
dark
|
str
|
Hex colour for the dark modules (default: deep navy |
'#1a1a2e'
|
light
|
str
|
Hex colour for the light modules (default: white |
'#ffffff'
|
file_format
|
str | None
|
Override the output format (e.g. |
None
|
Example
save_qr( ... "https://ipn.eg/S/alice/instapay/2DcFGv", ... "alice_qr.png", ... scale=12, ... dark="#005f5f", ... )
Source code in src/instapay_eg/integrations/qrcode.py
qr_as_bytes
¶
qr_as_bytes(
link: str,
*,
file_format: str = "png",
scale: int = 10,
dark: str = "#1a1a2e",
light: str = "#ffffff",
) -> bytes
Return the QR code image for link as raw bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
link
|
str
|
A validated |
required |
file_format
|
str
|
Image format. |
'png'
|
scale
|
int
|
Pixel size of each QR module. |
10
|
dark
|
str
|
Hex colour for the dark modules. |
'#1a1a2e'
|
light
|
str
|
Hex colour for the light modules. |
'#ffffff'
|
Returns:
| Type | Description |
|---|---|
bytes
|
Raw image bytes in the requested format. |
Example
png_bytes = qr_as_bytes("https://ipn.eg/S/alice/instapay/2DcFGv") with open("payment.png", "wb") as f: ... f.write(png_bytes)
Source code in src/instapay_eg/integrations/qrcode.py
qr_as_base64
¶
qr_as_base64(
link: str,
*,
file_format: str = "png",
scale: int = 10,
dark: str = "#1a1a2e",
light: str = "#ffffff",
) -> str
Return the QR code as a base64-encoded string, ready for a JSON API.
The returned string can be embedded directly in an HTML <img> tag or
returned from a REST endpoint without needing to write a file to disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
link
|
str
|
A validated |
required |
file_format
|
str
|
Image format used for encoding. |
'png'
|
scale
|
int
|
Pixel size of each QR module. |
10
|
dark
|
str
|
Hex colour for the dark modules. |
'#1a1a2e'
|
light
|
str
|
Hex colour for the light modules. |
'#ffffff'
|
Returns:
| Type | Description |
|---|---|
str
|
A base64-encoded string of the QR code image (no |
Example
b64 = qr_as_base64("https://ipn.eg/S/alice/instapay/2DcFGv") html = f'
'
Source code in src/instapay_eg/integrations/qrcode.py
qr_as_svg_string
¶
qr_as_svg_string(
link: str,
*,
scale: int = 10,
dark: str = "#1a1a2e",
light: str = "#ffffff",
) -> str
Return the QR code as an inline SVG string.
The returned string can be injected directly into an HTML document without a separate HTTP request, and scales perfectly at any resolution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
link
|
str
|
A validated |
required |
scale
|
int
|
Size multiplier for the SVG viewport. |
10
|
dark
|
str
|
Hex colour for the dark modules. |
'#1a1a2e'
|
light
|
str
|
Hex colour for the light modules. |
'#ffffff'
|
Returns:
| Type | Description |
|---|---|
str
|
A complete |
Example
svg = qr_as_svg_string("https://ipn.eg/S/alice/instapay/2DcFGv") html_response = f"
{svg}"
Source code in src/instapay_eg/integrations/qrcode.py
Django¶
instapay_eg.integrations.django
¶
Django integration for instapay-eg.
Provides a Django ORM model field and a Django form field for seamless integration with Django applications.
Usage in a Django model::
from django.db import models
from instapay_eg.integrations.django import InstaPayLinkField
class UserProfile(models.Model):
instapay_link = InstaPayLinkField(blank=True, null=True)
Usage in a Django form::
from django import forms
from instapay_eg.integrations.django import InstaPayHandleFormField
class PaymentForm(forms.Form):
recipient = InstaPayHandleFormField()
Raises:
| Type | Description |
|---|---|
ImportError
|
If |
InstaPayLinkField
¶
Bases: CharField
A Django model field that stores a validated InstaPay payment link.
On full_clean() / save(), the field automatically:
- Extracts the
https://ipn.egURL from the raw input (so users can paste the full share-sheet text directly). - Runs all security checks (HTTPS, domain, phishing patterns).
- Stores only the clean URL - not the surrounding text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_length
|
int
|
Column length. Defaults to |
500
|
**kwargs
|
Any
|
Passed directly to :class: |
{}
|
Example::
class Professional(models.Model):
instapay_link = InstaPayLinkField(blank=True, null=True)
Source code in src/instapay_eg/integrations/django.py
__init__
¶
Initialise the field with a sensible default max_length.
clean
¶
Validate and clean the field value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The raw input value. |
required |
model_instance
|
Any
|
The model instance being saved. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
The validated InstaPay URL, or |
str | None
|
and the value is empty. |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If the link is missing, insecure, or malformed. |
Source code in src/instapay_eg/integrations/django.py
InstaPayHandleFormField
¶
Bases: CharField
A Django form field that validates an InstaPay handle.
Accepts both 'alice' and 'alice@instapay' - the @instapay
suffix is stripped and normalised automatically.
Example::
class PaymentForm(forms.Form):
recipient = InstaPayHandleFormField(
label="InstaPay Handle",
help_text="Enter the recipient's @instapay handle.",
)
Source code in src/instapay_eg/integrations/django.py
validate
¶
Validate that value is a correctly formatted InstaPay handle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
The submitted form value. |
required |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If the handle contains invalid characters. |
Source code in src/instapay_eg/integrations/django.py
clean
¶
Normalise and validate the handle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
The submitted form value. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The normalised handle (without |