Hey there, fellow developer! Ready to supercharge your web forms? Let's dive into the world of 123FormBuilder API integration. This powerful tool will let you create, manage, and analyze forms with just a few lines of JavaScript. Buckle up!
Before we jump in, make sure you've got:
Let's get our project off the ground:
mkdir 123form-integration && cd 123form-integration npm init -y npm install axios dotenv
Create a .env
file and add your API key:
API_KEY=your_api_key_here
Time to get cozy with the API. Create an index.js
file:
require('dotenv').config(); const axios = require('axios'); const api = axios.create({ baseURL: 'https://api.123formbuilder.com/v2', headers: { 'Authorization': `Bearer ${process.env.API_KEY}` } });
Let's test the waters with a simple GET request:
async function getForms() { try { const response = await api.get('/forms'); console.log(response.data); } catch (error) { console.error('Error fetching forms:', error.response.data); } } getForms();
Ready to birth a new form? Here's how:
async function createForm() { try { const newForm = { name: 'My Awesome Form', fields: [ { type: 'text', label: 'Name' }, { type: 'email', label: 'Email' } ] }; const response = await api.post('/forms', newForm); console.log('Form created:', response.data); } catch (error) { console.error('Error creating form:', error.response.data); } }
Let's fetch those valuable submissions:
async function getSubmissions(formId) { try { const response = await api.get(`/forms/${formId}/submissions`); console.log('Submissions:', response.data); } catch (error) { console.error('Error fetching submissions:', error.response.data); } }
Time for a form makeover:
async function updateForm(formId) { try { const updates = { name: 'My Even More Awesome Form', fields: [ { type: 'text', label: 'Full Name' }, { type: 'email', label: 'Email Address' }, { type: 'textarea', label: 'Comments' } ] }; const response = await api.put(`/forms/${formId}`, updates); console.log('Form updated:', response.data); } catch (error) { console.error('Error updating form:', error.response.data); } }
When it's time to say goodbye:
async function deleteForm(formId) { try { await api.delete(`/forms/${formId}`); console.log('Form deleted successfully'); } catch (error) { console.error('Error deleting form:', error.response.data); } }
Let's wrap our API calls in a tidy little package:
async function apiCall(method, endpoint, data = null) { try { const response = await api[method](endpoint, data); return response.data; } catch (error) { console.error(`API Error (${method.toUpperCase()} ${endpoint}):`, error.response.data); throw error; } }
Want real-time updates? Set up a webhook:
async function setupWebhook(formId, webhookUrl) { try { const webhook = { url: webhookUrl, events: ['form.submitted'] }; const response = await api.post(`/forms/${formId}/webhooks`, webhook); console.log('Webhook set up:', response.data); } catch (error) { console.error('Error setting up webhook:', error.response.data); } }
Here's a quick test suite to get you started:
const assert = require('assert'); async function runTests() { // Test creating a form const form = await apiCall('post', '/forms', { name: 'Test Form' }); assert(form.id, 'Form creation failed'); // Test updating the form const updatedForm = await apiCall('put', `/forms/${form.id}`, { name: 'Updated Test Form' }); assert(updatedForm.name === 'Updated Test Form', 'Form update failed'); // Test deleting the form await apiCall('delete', `/forms/${form.id}`); try { await apiCall('get', `/forms/${form.id}`); assert(false, 'Form deletion failed'); } catch (error) { assert(error.response.status === 404, 'Form deletion failed'); } console.log('All tests passed!'); } runTests();
And there you have it! You're now armed with the knowledge to create a robust 123FormBuilder API integration. Remember, this is just scratching the surface – there's a whole world of form customization and data analysis waiting for you in the official API documentation.
Now go forth and create some killer forms! Happy coding! 🚀