Skip to main content

Python SDK for Forminit

Project description

Forminit Python SDK

A lightweight, Python SDK for submitting forms to Forminit.

Features

  • 🐍 Pythonic API - Simple, intuitive interface
  • Sync & Async - Both synchronous and asynchronous clients
  • 🔒 Type Safe - Full type hints and TypedDict definitions
  • 🧪 Well Tested - Comprehensive test suite with pytest
  • 🚀 Framework Agnostic - Works with Flask, FastAPI, Django, or any Python app

Installation

pip install forminit

Quick Start

Synchronous Usage

from forminit import ForminitClient

# Initialize client with your API key
client = ForminitClient(api_key="your-api-key")

# Set user info for tracking (optional)
# Note: Set user info from the client request for accurate tracking
# This ensures proper geolocation and analytics for the actual user
client.set_user_info(
    ip="203.0.113.10",  # Client's IP from X-Forwarded-For or X-Real-IP header
    user_agent="Mozilla/5.0...",  # Client's User-Agent header
    referer="https://example.com",  # Client's Referer header
)

# Submit form data
result = client.submit(
    form_id="your-form-id",
    data={
        "blocks": [
            {"type": "text", "name": "message", "value": "Hello!"},
            {"type": "email", "name": "email", "value": "user@example.com"},
        ]
    },
    tracking={"utmSource": "newsletter", "utmMedium": "email"},
)

if "error" in result:
    print(f"Error: {result['error']['message']}")
else:
    print(f"Success! Submission ID: {result['data']['hashId']}")
    if "redirectUrl" in result:
        print(f"Redirect to: {result['redirectUrl']}")

client.close()

Web Framework Example

When using in web frameworks, always extract client information from the request:

from flask import Flask, request
from forminit import ForminitClient

app = Flask(__name__)

@app.route("/submit", methods=["POST"])
def submit():
    client = ForminitClient(api_key="your-api-key")
    
    # Extract client information from request headers for accurate tracking
    # Note: Use X-Forwarded-For when behind proxies/load balancers
    client_ip = request.headers.get("X-Forwarded-For", request.remote_addr)
    if client_ip and "," in client_ip:
        client_ip = client_ip.split(",")[0].strip()  # Get first IP
    
    client.set_user_info(
        ip=client_ip,
        user_agent=request.headers.get("User-Agent", ""),
        referer=request.headers.get("Referer"),
    )
    
    result = client.submit(
        form_id="your-form-id",
        data=request.form.to_dict(),
    )
    
    client.close()
    return result

Asynchronous Usage

import asyncio
from forminit import AsyncForminitClient


async def main():
    async with AsyncForminitClient(api_key="your-api-key") as client:
        result = await client.submit(
            form_id="your-form-id",
            data={
                "blocks": [
                    {"type": "text", "name": "message", "value": "Hello!"},
                ]
            },
        )

        if "error" in result:
            print(f"Error: {result['error']['message']}")
        else:
            print(f"Success! Submission ID: {result['data']['hashId']}")


asyncio.run(main())

Form-Style Submission

You can also submit flat form data (like from an HTML form):

result = client.submit(
    form_id="your-form-id",
    data={
        "fi-sender-fullName": "John Doe",
        "fi-sender-email": "john@example.com",
        "fi-text-message": "Hello!",
    },
)

Configuration

Client Options

from forminit import ForminitClient

client = ForminitClient(
    api_key="your-api-key",  # Required for server-side usage
    proxy_url=None,           # Optional: proxy URL for client-side usage
    base_url="https://forminit.com",  # Optional: override base URL
)

Environment Variables

export FORMINIT_API_KEY="your-api-key"
export FORMINIT_FORM_ID="your-form-id"

Types

The SDK includes comprehensive type definitions:

from forminit.types import (
    FormBlock,
    FormSubmissionData,
    FormResponse,
    TrackingBlock,
    SenderBlock,
    TextBlock,
    # ... and more
)

# Type-safe form building
submission: FormSubmissionData = {
    "blocks": [
        {
            "type": "sender",
            "properties": {
                "email": "user@example.com",
                "firstName": "John",
                "lastName": "Doe",
            },
        },
        {"type": "text", "name": "message", "value": "Hello!"},
    ]
}

Block Types

The SDK supports all Forminit block types:

  • tracking - UTM and ad tracking parameters
  • sender - User information (email, name, etc.)
  • text - Text input
  • number - Number input
  • email - Email input
  • url - URL input
  • phone - Phone input
  • rating - Rating input
  • date - Date input
  • select - Select/dropdown
  • radio - Radio button (single selection)
  • checkbox - Checkbox (multiple selection)
  • file - File upload
  • country - Country selection

Examples

See the examples/ directory for complete working examples:

  • Flask - Flask web application
  • FastAPI - FastAPI async application
  • Django - Django web application

Running Examples

Each example includes its own README with setup instructions.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

forminit-0.1.0.tar.gz (71.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

forminit-0.1.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file forminit-0.1.0.tar.gz.

File metadata

  • Download URL: forminit-0.1.0.tar.gz
  • Upload date:
  • Size: 71.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for forminit-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c2a30738f517dd91f615d390aeb59b606e9d36c0ce7b829d46e59b401faa2bac
MD5 8f0616e09f2d84d113df422b9d51fd5d
BLAKE2b-256 47498a49c05cd8fca5ddc21bbbceb481a820ab71a0a4550cf81b5327005d2941

See more details on using hashes here.

File details

Details for the file forminit-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: forminit-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for forminit-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a4bc73979f7760b0b27260f2dcfa3218333ae937d16bb51fe9088b3f8d443d76
MD5 f1e10ec8d48d21ecb5c265c211c87f65
BLAKE2b-256 ecb279a061d2b16c1beed252a4e8940254bc92562417a6101a3b78ce9af31920

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page