Components
Drop-in components for account onboarding
This guide is designed to help platform developers seamlessly embed Straddle’s instant onboarding components into your application
Implementation Overview
Before embedding a Straddle form, you need to create an organization for each of your clients and obtain its unique ID (organization_id
). This ID links the form to the specific organization within your platform.
Creating an Organization
Make a POST
request to the Straddle API to create an organization:
-
Authorization: Replace
<api-key>
with your actual API key. -
Data Fields:
-
name
: The name of your client’s organization. -
metadata
: An optional object for additional data. -
external_id
: An optional external identifier for your internal mapping.
-
Sample API Response
- Organization ID (
organization_id
): Theid
field (3c90c3cc-0d44-4b50-8888-8dd25736052a
) is your client’s unique organization ID. You’ll use this ID in the embed URL.
Embedding Approaches
Approach 1: Quick Embed
This approach is ideal for developers seeking a plug-and-play solution. Simply add the script and iframe, and the embed loads automatically.
Full Example Usage
Note: Replace {organization_id}
with your client’s actual organization ID obtained from the API response.
How It Works
-
Iframe Setup: The iframe uses the
data-straddle-src
attribute to hold the embed URL, including your client’sorganization_id
. Thesrc
attribute is initially omitted to prevent immediate loading. -
Initialization Script: The script checks for the presence of the Straddle script and loads it if necessary. It then initializes the embeds by setting the
src
attribute of each iframe. -
Automatic Loading: This method is ideal when you want the embed to load automatically without any additional code or user interaction.
Approach 2: Dynamic Loading
For greater control over when the embed loads, use the Dynamic Loading approach. This method allows you to decide exactly when to initialize the embeds by calling Straddle.loadEmbeds()
.
Example Usage
Step 1: Include the Straddle Embed Initialization Script
Step 2: Add the Straddle Embed Iframe
Note: Replace {organization_id}
with your client’s actual organization ID.
Step 3: Load Embeds Dynamically
Call the Straddle.loadEmbeds()
function at the point in your code where you want the embeds to load.
Option A: Load on Page Load
Option B: Load on User Interaction
How It Works
-
Initialization Script:
-
Defines a global
Straddle
object if it doesn’t already exist. -
Adds a
loadEmbeds
method that sets thesrc
attribute of all iframes withdata-straddle-src
, triggering the embeds to load. -
Includes compatibility checks for older browsers and uses strict mode.
-
-
Manual Control:
- You decide when to call
Straddle.loadEmbeds()
, giving you flexibility to load embeds based on user actions or specific conditions in your platform.
- You decide when to call
-
Embed Loading:
- The
loadEmbeds
function processes all relevant iframes, ensuring they load correctly across different browsers.
- The
Approach 3: React Integration
For platforms built with React, this approach demonstrates how to integrate Straddle Embed within a React component, giving you dynamic control over the embedding process.
Example Usage
Usage Example
How It Works
-
Component Initialization: The
useEffect
hook ensures the embed script is loaded when the component mounts. -
Script Loading:
-
Checks if the Straddle script is already present.
-
If not, it dynamically adds the script to the document.
-
Adds event listeners to handle the script’s
load
anderror
events.
-
-
Embed Loading: Calls
loadEmbeds()
to initialize the embeds once the script is loaded. -
Cleanup: Removes event listeners when the component unmounts to prevent memory leaks.
Best Practices
-
Script Placement: Always include the embed initialization script before attempting to load the embeds.
-
Replace Placeholders: Ensure you replace
{organization_id}
with your client’s actual organization ID in the iframedata-straddle-src
attribute. -
Control Loading: Use the Manual Loading approach if you need to load the embed after specific conditions are met.
-
Error Handling: Monitor the console for error messages to troubleshoot any issues during development.
-
Browser Compatibility:
-
If supporting older browsers, ensure your code is compatible or use polyfills where necessary.
-
The provided code includes fallbacks for features like
NodeList.forEach
anddataset
.
-
-
Security Considerations:
-
Sanitize any dynamic data used in URLs to prevent injection attacks.
-
Ensure that
organizationId
values are validated.
-
-
Performance Optimization:
-
Load the embed script asynchronously to prevent blocking page rendering.
-
Consider lazy loading embeds when they are needed to improve initial load times.
-
-
Debugging:
- Use the
isDebugMode
flag to enable or disable detailed logging during development.
- Use the
Notes
-
Embed Script URL: The Straddle embed script is available at
https://forms.straddle.io/embed.js
. -
Iframe Customization:
-
Adjust URL parameters (
alignLeft
,hideTitle
,transparentBackground
, etc.) to customize the form’s appearance. -
Set iframe attributes (
width
,height
,title
) as needed for your layout.
-
Appendix: Error Handling and Debugging
-
Logging:
-
The scripts include
console.error
statements to assist in debugging. -
Use the
isDebugMode
flag to enableconsole.log
statements during development. -
Set
isDebugMode
tofalse
in production to avoid cluttering the console.
-
-
Error Messages:
-
The scripts provide detailed error messages to help troubleshoot issues.
-
For example,
"Straddle: Failed to load embeds. Error:"
followed by the error details.
-
-
Fallback Mechanisms:
-
The code includes fallbacks to ensure that if the Straddle script fails to load, the embeds are still initialized manually.
-
In the React example, the
loadEmbeds
function is called even if the script fails to load, attempting to load the embeds manually.
-
-
Browser Compatibility:
- The code includes checks and fallbacks for features not supported in older browsers (e.g.,
NodeList.forEach
,dataset
).
- The code includes checks and fallbacks for features not supported in older browsers (e.g.,
Final Remarks
By following this guide and using the provided code snippets, you should be able to integrate Straddle forms into your platform smoothly, allowing you to onboard your clients efficiently. Whether you need a quick embed solution, manual control over loading, or integration within a React application, Straddle provides the flexibility to meet your development needs.
Remember to:
-
Replace placeholder values with your actual data.
-
Handle errors appropriately and monitor console messages during development.
-
Test your integration thoroughly to ensure the best experience for your clients.