Hey there, fellow developer! Ready to supercharge your forms with the 123FormBuilder API? This guide will walk you through integrating this powerful tool into your Python projects. We'll cover everything from basic setup to advanced features, so buckle up and let's dive in!
Before we get our hands dirty, make sure you've got:
requests
library (pip install requests
)Let's kick things off by importing our modules and setting up our API connection:
import requests import json API_KEY = 'your_api_key_here' BASE_URL = 'https://api.123formbuilder.com/v2' HEADERS = { 'Authorization': f'Bearer {API_KEY}', 'Content-Type': 'application/json' }
Want to fetch your form data? It's as easy as pie:
def get_form(form_id): response = requests.get(f'{BASE_URL}/forms/{form_id}', headers=HEADERS) return response.json()
Time to create a shiny new form:
def create_form(form_data): response = requests.post(f'{BASE_URL}/forms', headers=HEADERS, json=form_data) return response.json()
Need to tweak your form? We've got you covered:
def update_form(form_id, updated_data): response = requests.put(f'{BASE_URL}/forms/{form_id}', headers=HEADERS, json=updated_data) return response.json()
Sometimes you gotta let go:
def delete_form(form_id): response = requests.delete(f'{BASE_URL}/forms/{form_id}', headers=HEADERS) return response.status_code == 204
Always check your responses to keep things smooth:
def handle_response(response): if response.status_code == 200: return response.json() else: raise Exception(f"API request failed: {response.status_code} - {response.text}")
Stay in the loop with webhooks:
def set_webhook(form_id, webhook_url): webhook_data = {'url': webhook_url, 'events': ['form.submit']} response = requests.post(f'{BASE_URL}/forms/{form_id}/webhooks', headers=HEADERS, json=webhook_data) return response.json()
Handling file uploads is a breeze:
def upload_file(form_id, file_path): with open(file_path, 'rb') as file: files = {'file': file} response = requests.post(f'{BASE_URL}/forms/{form_id}/files', headers=HEADERS, files=files) return response.json()
Always test your integration thoroughly:
def test_api_connection(): response = requests.get(f'{BASE_URL}/forms', headers=HEADERS) assert response.status_code == 200, "API connection failed"
And there you have it! You're now equipped to harness the full power of 123FormBuilder in your Python projects. Remember, practice makes perfect, so don't be afraid to experiment and push the boundaries of what you can create.
Happy coding, and may your forms be ever responsive!
For a complete working example, check out our GitHub repository. Feel free to fork, star, and contribute!