BeeHiiv Integrations: How To Connect Your Own Forms To BeeHiiv (Using The API)
In this post, I will show you how to integrate any forms on your site with BeeHiiv. Including the PHP code needed to use the BeeHiiv API to subscribe leads to your BeeHiiv newsletter.
In This Post…
BeeHiiv opt-in forms are rather limited. So, what if you could have your own forms on your site that send leads into your newsletter email list?
BeeHiiv is great. I am now using it for both The Morning WP and Happily RV. For newsletters, this is the platform to use (in my opinion). (And yes, for your in-house CRM, I still very much recommend FluentCRM).
Thing is… BeeHiiv is young. They are iterating fast and adding new functionality quickly. But, it’s relative youth means there’s not a lot of official BeeHiiv integrations yet.
For opt-in forms, they do provide embeddable opt-in forms you can use on your own site. However, they’re very inflexible. Plus, if you’re anything like me, you want to not 100% rely on BeeHiiv. You want to also put leads into your CRM and better integrate your own tech stack.
The great thing is… BeeHiiv does have an API.
You do need a paid account to access the API, but if you do have access to their API, it opens up the ability to integrate with almost anything you want.
In my case, I have already integrated Fluent Forms as well as ConvertBox.
The forms on this site are built with Fluent Forms. They now send leads into BeeHiiv as well as FluentCRM simultaneously.
I built this integration so that I control it. I am not relying on something like Zapier or Make.com (although these are options, too.)
I will share the code with you. You’re going to need to be a bit more technically inclined to implement this, since I don’t intend to cover every little nitty-gritty detail.
If you would like my assistance implementing and adjusting this to your own site, then you can sign on to have me do it for you. One service credit should be adequate.
Let’s go…
The PHP Code To Subscribe A Person To Your BeeHiiv Newsletter Using Their API
Basically, what we’re going to do is create a PHP file on our server that will act as a webhook.
You’re going to need two pieces of info from your BeeHiiv account:
- Publication ID
- API Key
Both can be gotten from Settings > Integrations within your BeeHiiv account. You will need to create a new API key for this script. The publication ID will need to be inserted into the URL for the API endpoint.
OK, here’s the code for the script:
<?php
// Replace these with your actual Beehiiv API credentials and endpoint
$apiEndpoint = 'https://api.beehiiv.com/v2/publications/[PUB KEY]/subscriptions';
$apiKey = '[API KEY]';
$email = $_GET[email];
$referring_site = $_GET[referring_site];
$utm_campaign = $_GET[utm_campaign];
// Subscriber data
$subscriberData = array(
'email' => $email,
'reactivate_existing' => true,
'send_welcome_email' => true,
'utm_source' => 'bma',
'utm_campaign' => $utm_campaign,
'utm_medium' => 'form',
'referring_site' => 'https://www.yoururl.com'
// Add other subscriber data fields as needed
);
// Prepare POST data
$postData = json_encode($subscriberData);
// Create a new cURL resource
$ch = curl_init($apiEndpoint);
// Set cURL options
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer '. $apiKey
));
// Execute cURL request
$response = curl_exec($ch);
// Close cURL resource
curl_close($ch);
?>
You will need to drop the publication key and API key into the code at the top. Obviously change the URL for the “referring site” to be your own (this way you will know subscribers came from your own site).
Save this code to a PHP file. Upload it somewhere on your server via FTP. Note the filename and URL path to it because you’re going to need it.
How To Connect Fluent Forms With BeeHiiv
In short, you’re going to use the built-in webhook functionality of Fluent Forms to send the field data from your form off to your webhook. The webhook URL is the full URL to the script you just uploaded.
This method isn’t unique to Fluent Forms. It would work with any forms builder that can send webhooks (which is most of them).
First, you need to enable the webhook integration for Fluent Forms. It isn’t turned on by default.
With webhooks turned on, you just go to create a new webhook integration under the “Settings & Integrations” section for your form. Here’s what mine looks like:
You can pass variables out of your form using either GET or POST variables. Either would work, but I went ahead and used GET.
Instead of passing all form fields, I passed only the ones I needed. If you needed to capture additional information, you’d need to pass it into BeeHiiv using custom fields. With BeeHiiv, even the name is considered a custom field. So, if you wanted to collect any information besides the email address, you’d need to pass them along as custom fields. The script would need to be modified in order to pass that data onto the API in the proper format.
Note that I am using the “utm_campaign” field in this example for my own internal purposes. It is meant to track UTM tags. However, I am passing through a hidden field that contains the name of the lead magnet they’re opting in for. This way I can segment people within BeeHiiv depending on the lead magnet.
How I Made Fluent Forms Look Like The BeeHiiv Opt-in Forms
The BeeHiiv open-in forms are pretty simplistic, but they look nice. And, in most cases, all we need to ask for is their email addresss. These are very simple forms.
When you build your opt-in form with Fluent Forms, it isn’t going to look anything like that. However, with some CSS modifications, we can make a form built with Fluent look like that.
For your form settings in Fluent, go to Custom CSS/JS. And here is the code that I dropped into that field:
.fluent_form_FF_ID fieldset {
display: flex;
}
.fluent_form_FF_ID input[type="email"] {
min-height: 60px;
padding: 16px;
border: 1px solid #a0aec0;
border-radius: 5px 0px 0px 5px;
}
.fluent_form_FF_ID {
display: flex;
padding: 4px;
}
.fluent_form_FF_ID .ff_submit_btn_wrapper button {
min-width: 140px;
color: #fff;
height: 100%;
padding: 12px 24px;
border-radius: 0px 5px 5px 0px;
}
.fluent_form_FF_ID .ff-el-group {
margin-bottom: 0 !important;
display: flex;
align-items: center;
}
.fluent_form_FF_ID .ff-el-input--content {
width: 100%;
}
.fluent_form_FF_ID .ff-el-group:not(:last-child) {
width: 100% !important;
}
.fluent_form_FF_ID .ff-el-form-control {
width: 100%;
font-size: 16px !important;
transition: border-color .15s ease-in-out,box-shadow .15s ease-in-out;
max-width: 100%;
}
In this case, you don’t need to worry about putting specific form IDs into the CSS because Fluent Forms will dynamically replace it. This CSS code will make the Fluent Form look like this:
Looks pretty much the same. ๐ However, we can make far more modifications to this one since it is Fluent Forms.
How I Integrated ConvertBox With BeeHiiv
ConvertBox provides all kinds of flexibility for calls to action. It is really handy and easy to use for opt-in forms. And now that we have ourselves a webhook endpoint that can send leads into BeeHiiv, we have what we need to integrate ConvertBox and BeeHiiv.
In fact, webhooks is also how you can integrate ConvertBox and FluentCRM. This is how we can accomplish the dual opt-in of putting people onto our local list and BeeHiiv at the same time.
First off, you create your ConvertBox any way that you want. Your form should include just the email address for the sake of simplicity. As I previously mentioned, anything else would require use of custom fields.
Once you have the form, you go to Forms Settings > Integrations. Then, go to Add Integration.
In the dropdown, you can select a webhook.
In the next field, you just enter the URL to your webhook.
Now, in my case, I found that I needed a separate script for this than what I was using for Fluent Forms. This is because ConvertBox formats data via JSON before being sent to the script. You could do the same with Fluent Forms, but I was in a hurry and just created a second web hook.
Here’s the PHP code for the same basic webhook that expects JSON-formatted input:
<?php
// Replace these with your actual Beehiiv API credentials and endpoint
$apiEndpoint = 'https://api.beehiiv.com/v2/publications/[PUB KEY]/subscriptions';
$apiKey = '[API KEY]';
$json_str = file_get_contents('php://input');
$json_obj = json_decode($json_str);
$email = $json_obj->{'email'};
$referring_site = $json_obj->{'$referer'};
$utm_campaign = $json_obj->{'utm_campaign'};
// Subscriber data
$subscriberData = array(
'email' => $email,
'reactivate_existing' => true,
'send_welcome_email' => true,
'utm_source' => 'bma',
'utm_campaign' => $utm_campaign,
'utm_medium' => 'form',
'referring_site' => 'https://www.yoururl.com'
// Add other subscriber data fields as needed
);
// Prepare POST data
$postData = json_encode($subscriberData);
// Create a new cURL resource
$ch = curl_init($apiEndpoint);
// Set cURL options
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer '. $apiKey
));
// Execute cURL request
$response = curl_exec($ch);
// Close cURL resource
curl_close($ch);
?>
Once you have this webhook set up and your ConvertBox form pointed to it, then all leads from that box will be sent through your webhook… right into BeeHiiv. Pretty cool!
How To Integrate Anything Into BeeHiiv
The BeeHiiv API allows pretty much anything to integrate. You just need to know how to use it… and the code provided above is the basic PHP code needed to built your own webhook to do exactly that.
Any software that can use webhooks can then integrate with BeeHiiv.
I fully expect more forms plugins for WordPress to release “official” integrations with BeeHiiv as time goes on. You’ll probably begin to see opt-in form plugins offer integrations. But, in the end, all of them will use something similar to what I just gave you. While my code above is simplistic and a bit “dirty”… it works. And any of these other plugins are simply going to use the API just like I did.
So, any form on your site can technically send leads into your BeeHiiv email list as long as you can send off webhooks. That’s the only requirement.
Want This Set Up For You?
OK, even though I have pretty much given away the guts of how this was done above (since there’s honestly nothing special about it), I know this is still nerdy as hell for some people.
If you have adequate technical experience to take the above code and get things set up for yourself, then great!
If not, then I can do it for you.
You will need one service credit on your account with the Blog Marketing Academy. We’ll then use that credit and I will implement this for your site and make sure everything is working as needed.
Keep in mind, you must have a paid BeeHiiv account in order to make use of the API. I simply won’t be able to set this up for you if you’re using their free account.
Got A Question?
Have a question about this article? Need some help with this topic (or anything else)? Send it in and I’ll get back to you personally. I think that’s better than a blog comment. ๐