Skip to main content
The OpnForm JavaScript SDK enables you to programmatically control embedded forms, listen to events, and build dynamic integrations. It includes automatic iframe resizing and full backward compatibility with existing embeds.

Installation

Include the SDK script after your form iframe:
The SDK automatically discovers OpnForm iframes on your page and initializes them. No additional setup required.

Automatic campaign attribution

The SDK automatically captures supported campaign parameters from the parent page and stores them with the submission under meta.attribution. Parameters already present in the iframe src take precedence over values from the parent page. Supported campaign parameters are utm_source, utm_medium, utm_campaign, utm_id, utm_term, utm_content, utm_source_platform, utm_creative_format, utm_marketing_tactic, gclid, gbraid, wbraid, dclid, fbclid, ttclid, and msclkid. Only these parameters are transmitted to the form. Other URL parameters and the complete parent URL are not captured. A raw cross-origin iframe without the SDK can only capture parameters included directly in its own src.

Quick Start


Events

Listen to form events to trigger custom actions, send data to analytics, or integrate with your application.

Available Events

Listening to Events

Removing Event Listeners


Form Methods

Access form instances using opnform.get('form-slug') and call methods to control the form.

Field Operations

Set the value of a specific field.
Set multiple field values at once.
Get the current value of a field.
Get all current form data.
Clear field values.

Error Handling

Theme Control

Form Actions

For forms embedded as popups:

Global SDK Methods

Form Management

Initialization Options

Programmatic Form Creation

Use opnform.create(slugOrUrl, options) to create an iframe for an existing form. Pass either its slug or its full share URL. With a slug, the iframe loads from https://opnform.com by default, regardless of your page’s domain or where you load the SDK script.
If you have the form’s share URL, pass it directly. This works with OpnForm Cloud, custom domains, subdomains, and self-hosted instances:
The URL must use http:// or https:// and point to a public form at /forms/{slug}. Its query parameters and fragment are preserved. You can access the resulting form with opnform.get("my-form-slug") as usual. If you only have the slug, pass baseUrl to select a custom domain or a self-hosted instance. Include the protocol:
This loads https://forms.example.com/forms/my-form-slug inside the iframe.
  • A full URL takes precedence over baseUrl; its host is never replaced.
  • With a slug, baseUrl defaults to https://opnform.com. The SDK does not look up the form’s custom domain automatically.
  • An explicit darkMode option overrides the value in the URL. The SDK sets its own _sdkToken and _sdkParentOrigin parameters for iframe communication.
  • Existing iframes keep their own src.
If your integration previously relied on create() loading from the page’s domain, pass that instance’s baseUrl explicitly or use the full share URL.

Integration Examples

Google Analytics 4

Send to Custom API

Error Tracking

Dynamic Field Population


Custom Code Integration

The SDK is automatically available when using the Custom Code feature in OpnForm. You can add custom JavaScript directly in your form or workspace settings, and the window.opnform SDK will be ready to use.
Custom Code works without iframes - the SDK is initialized directly on your form page, giving you full access to form methods and events.

Adding Custom Code

  1. Go to your form’s Settings → Custom Code (or Workspace Settings for workspace-wide code)
  2. Add your JavaScript code in a <script> tag
  3. The SDK (window.opnform) is automatically available

Example: Google Analytics Tracking

Example: Facebook Pixel

Example: Conditional Logic with External Data

Example: Custom Validation Feedback

Example: Live Data Change Tracking

Accessing Form Data Directly


Backward Compatibility

Existing embeds using initEmbed() continue to work without changes. The SDK provides full backward compatibility.
We recommend upgrading to the new SDK to access event callbacks and programmatic control features.

Troubleshooting

Ensure the iframe has loaded and the id attribute matches the form slug:
Use opnform.getAll() to see discovered forms.
Make sure the SDK script is loaded after the iframe:
Commands require the form to be ready. Use the ready event:
The SDK includes iFrame Resizer. If resize isn’t working, ensure:
  • No CSS max-height restricting the iframe
  • The form is not in focused presentation style (which has fixed height)