How to Build a Micro SaaS? With Lemon Squeezy!

I know PHP and I will do this training on PHP. But I can assure you that I will explain how it can be used in different languages and as nocode in the article. Programming languages are similar. After you understand the logic, you can easily code with the language or framework you know. So have peace of mind. Let’s start.

Contents:

How to Build a Micro SaaS? with LemonSqueezy

Open a LemonSqueezy Account

First of all, your bedside book will be the LemonSqueezy API Document. I suggest you read it from start to finish once. At least something comes to mind. Otherwise, you may fall into the mistakes I made. You will understand what I mean in the article. Happy reading. 🙂

Once you open an account on LemonSqueezy, you cannot use it immediately. They review your account and your business so you will have to wait a while. This is standard use.

LemonSqueezy has a test mode. In this way, you can test without depositing money. For this, activate the “Test mode” button on the sidebar.

How to Generate API Key?

Since we are going to do Micro SaaS, we need to use API. Click “Settings > API” from the sidebar. On the page that opens, it says “Create a new API key to authenticate your app” under the “API keys” heading. Let’s create our first API by clicking the “+” button to its right.

When we click this button, it tells us to enter the API name. You can name it yourself here. I will write “UgurKILCIAPI“.

It will give you complex text in a text box when you create it. This is called an API key. This is very important. Do not lose this code because you only see this code once. If you haven’t saved this code somewhere, that is, the API key, you have to create it again. Because there is no API key display feature.

I closed it by saying “Close“. We see the keys under API keys. This means that more than one API key can be created. But the 2 API key is not our topic. Let’s not get off-topic.

Now let’s create a product.

How to Add Product to LemonSqueezy?

  • Once you’re in “Store > Products“, click the purple “+” button on the top right.
  • Two options will open. “New Product” and “New Discount“. I choose the “New Product” option.
  • Let’s write the name of our product. I am writing “UgurKILCI PRO Pack” here.
  • I leave a note in the description, just in case, to write the email you used on the website.
    Use the email you used on the website! Otherwise, we may not be able to recognize you! If you encounter a problem, you can send an email to elon@tesla.com.
  • Just below is the “Pricing” section. This place is important. Here you must select the “Subscriptionmodel.
  • Enter the subscription fee. I’ll write $49.
  • It tells the cycles that will happen with “Repeat payment every:”. You can do it once a month. In my previous Micro SaaS (I am no longer affiliated with that Micro SaaS), we also did 6-month cycles. (see: createtargetaudience.com)
  • Subscription has free trial?” If you select the option, you can add a trial period. After one first writes down the bank information, the trial process starts. I have never used this option. I’ll try it on my next Micro SaaS. 🙂 (My next Micro SaaS is a tool for making APIs with Notion. I’ve developed this tool before, but I couldn’t keep up with this speed because Notion APIs are updated very quickly. The system is not working at the moment. I will update it as it works as soon as possible. :)) For now, this option I keep closed.
  • Upload a 1600×1200 image in “Media“.
  • We have no business with the “Files” and “Variants” options.
  • We have nothing to do with the “Generate License Keys” option either.
  • But the “Redirect after purchase?” option works for us, so activate it and write the link of the file that will be redirected to your site after purchasing it into the text box that opens.
  • Now, this is very important!Redirect after purchase?” is very important. Because when someone buys, it will direct that person there. I need to tell you the logic here. I will explain it to those who want to do this by coding first. I’ll explain for nocode just below.

Micro SaaS Logic with PHP (+Programming Languages)

1) Detecting New Subscribers

  • Redirect after purchase?” It will be directed to the place we wrote in it. So sign up the user first. If you sign up you can use $_SESSION.
  • Add a sign-up button for those who do not log in on the “Pricing” page.
  • If you are a member, put the LemonSqueezy links on the packages.
  • The user first becomes a member and then purchases and asks “Redirect after purchase?” Goes to the link we wrote in it. If you remember, we told you to enter your e-mail address correctly in “Description“. For example, the buyer should be directed to the link of a file named “connect.php“. Check with $_SESSION whether the member or the visitor clicked first in that file.
  • The user first becomes a member and then purchases and asks “Redirect after purchase?” Goes to the link we wrote in it. If you remember, we told you to enter your e-mail address correctly in “Description“. For example, the buyer should be directed to the link of a file named “connect.php“. Check with $_SESSION whether the member or the visitor clicked first in that file.
  • Then write the code below. This code prints subscribers with API. We print all subscribers.

NOTE: Save the code below in a name like “api.php“, then open this file in “connect.php” with the $_SESSION control. This way you can use “api.php” in other places as well. (see: Cronjob)

// API key (access token)
$token = "Paste the API key here";

// List subscribers from Lemon API
curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://api-cors-anywhere.lemonsqueezy.com/v1/subscriptions/?page[number]=1&page[size]=100',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_HTTPHEADER => array(
    'Authorization: Bearer '.$token
  ),
));

$response = curl_exec($curl);
$response = json_decode($response, true);
curl_close($curl);

NOTE: You can also do this using Postman instead of PHP. The most beautiful feature of Postman is that it can print the output of the job in different programming languages. You can convert it to Javascript or Python or whatever you’re using.

This is important! We wrote this code to list 100 results on 1 page. We discovered this later. First, there were 10 listed on 1 page. When we had more than 10 users, users could either use it unlimitedly or not at all, even though they were members. When I examined the document files thoroughly, I realized that there was a paging system. Actually, the code works fine, but we set up the logic with only 10 results. So when I increased it to 100 results, the problem was solved. Of course, this will not depend on 100 results. It would be fine if you loop through the number of results as there will be more results in the future. I think you can loop this loop until the subscriber has an email. If you have a more alternative solution, please explain it in detail by commenting.

Information about subscribers is now listed in the $response. List using “foreach“.

foreach (array_reverse($response["data"]) as $row) { }

Now you’ll be asking why we use “array_reverse“. This is the result of pure experience. We had a Micro SaaS that my partner and I created, remember? (see: createtargetaudience.com) Here, with each purchase, another account was reset and the new subscriber’s limits were not updated. I looked at the codes etc, but I could not solve the problem somehow. That day my partner was angry with me for this mistake. 🙂 It was my first time to this point. There were things I didn’t know. This problem was one of them. Then I solved this problem. I will tell you how I solved this in a separate post. (coming soon: How to Detect Problems in Micro SaaS?) In Lemon, lists are listed as DESC. DESC means listing as 5,4,3,2,1… In other words, even if the last payer is 5, 1 is at the bottom. What can you say about it? But PHP has a code that works from top to bottom due to its structure.

For example $a = “Ugur”; then $a = “KILCI”; if it writes. echo $a; If you type it, the output will be “KILCI“. Retrieves the last data.

$a = "Ugur";
$a = "KILCI";
echo $a; // Output: KILCI

Because Lemon showed this last payment above, our codes were not working as we wanted. So I solved the situation by reversing the array. array_reverse is a hero. :p

  • There are 3 useful pieces of information in this list.
$product_id = $row["attributes"]["product_id"];
$user_email = $row["attributes"]["user_email"];
$status     = $row["attributes"]["status"];
  • First, we check the presence by email.
// Check if the subscriber exists in the system
$selectRow = $db -> prepare("SELECT * FROM users WHERE
    user_mail =:user_mail
");
$selectRow -> execute([
    'user_mail'=>$user_mail
]);
$selectRow = $selectRow -> rowCount();
 
if($selectRow > 0){
    // True
}else{
    // False
}
  • Then we get the member’s information from the database.
// Get that user's data
$data = $db -> prepare("SELECT * FROM users WHERE
    user_mail=?
");
$data -> execute([
    $user_mail
]);
$_data = $data -> fetch(PDO::FETCH_ASSOC);
  • This place is so important! I’m checking the $status!
if ($status == "active") {
    // This will work if the member becomes a new subscriber or renews the subscription.
} elseif($status == "cancelled") {
    // This will work when you unsubscribe.
}elseif($status == "expired") {
    // This place will work if he hasn't paid his subscription. Sometimes people use virtual cards and money cannot be withdrawn. This is a place made for it. I learned this when I emailed LemonSqueezy.
}elseif($status == "on_trial") {
    // This is a trial subscription. If he's a trial member, we can figure it out here and label his member confirmation as "trial member".
}
  • You can update the membership confirmation as 1 in the “active” section. In this way, you authorize the user. But at this point, I was screwed. 🙂 Some of you may have noticed, but I didn’t notice at first. 🙂 These kinda things happen. 🙂

    There is a Turkish proverb. I love it so much. “Kervan yolda düzülür (The caravan takes cargo on the way.)”.

    If you do not enter this path as a caravan, you will never notice such a mistake. Listen carefully! We list all subscribers! That means we’ll be updating their limits every time! That means limits or powers will be reset every time this file is called! It may have taken me a few weeks to detect and fix this. The solution is quite simple. For each purchase, add a column from your database that the person has purchased before and update it. (user_payment = 1) In this way, you will be able to check whether you have bought it before or not in that column. If purchased, no need to update again. If not, update it.

Now, this is something that can work in our system, but in other systems, problems may arise. For this reason, you can have a healthier system if you check it by adding +1 month to the date of the day of purchase and it with today’s date every cycle.

You can code this with simple logic:

date("Y-m-d", strtotime(date("Y-m-d") . "+1 month"));

Or

Lemon’s query includes “ends_at“. Here it shows when your subscription will expire. If you save this to your database, you can have a healthier structure. Let this place work if the subscription request is equal to or higher than today.

Okay, let’s briefly repeat what we’ve done so far.

After creating a product in Lemon and purchasing this product, we have listed our subscribers with cURL in the file to be redirected. We checked to see if the buyer’s information is in the database. And we detected purchase intent with $status. We are currently writing the contents of the “active” code.

  • What we do now is still important! Have the $product_id checked? We will write the ID of the package we created in Lemon and have it checked for equality to $product_id.

You can find the Product ID with the link to the package you created in Lemon.

// https://app.lemonsqueezy.com/products/12345

if ($product_id == 12345) { // First Pack (UgurKILCI PRO Pack)
    $user_conf = 1;
}elseif ($product_id == 6789) { // Second Pack (UgurKILCI PRO PLUS Pack)
    $user_conf = 2;
}elseif ($product_id == 1011) { // Third Pack (UgurKILCI Ultimate Pack)
    $user_conf = 3;
}

Now if you update them according to the email of the user/subscriber, you will have created and renewed the subscription.
It works the same way on Lemon and other platforms (rarely some don’t). With “active“, new subscribers and those who renew their subscriptions always send requests here. You do it once, you don’t have to look again. 🙂

2) How to Cancel Subscription?

If the user unsubscribes, you will check with $statuscanceled“. So set up a cronjob on your website and it will run “api.php” every day. Set the member confirmations to “0” for those whose $status is “canceled“. This way, you will take away your powers.

3) What Will Be Done in Case of non-payment of Subscription Fees in Extreme Cases?

Many situations can be encountered in financial technologies. Putting enough money to make a one-time payment with a virtual card is one of them. Or the user may actually run out of money. These are all extreme situations, and fintech takes these into account as well. We Micro SaaS manufacturers have to consider these as well. With $status == “expired” we should deprive him of his powers in these extreme situations, just like we did with “canceled”.

4) Trial Version

I do not use this feature for the trial version. Instead, I let them test without using a card. Some founders talk about the benefits of getting card information in advance, especially what I saw at MicroConf. In fact, it is healthier to do A/B testing. Maybe a different need arises on your platform. It needs to be detected.

Anyway, we can detect this again with if($status == “on_trial”). You can activate the trial membership here.

OK, that’s it. 🙂 You now have the Micro SaaS structure. You now have a subscription system. You can build your own tool on it.

You can also do what I explained above with other programming languages. Programming languages are similar. If you understand the algorithm below, you can figure out the rest.

How to Make Micro SaaS with No Code?

So, what kind of strategy should we apply to those who do not know to code? Let’s see what we can do.

For NoCode, LemonSqueezy has Webhook and Integrations. With these two things, you can make Micro SaaS without coding. Let’s start with Integrations first.

There are 3 options in “Settings > Integrations“. Mailchimp, ConvertKit and Zapier. From here, click the “Connect” button next to the Zapier option.
From the window that opens, click “Connect Lemon Squeezy to 5,000+ apps” and click the button. Select a trigger with “Trigger > Event“.

LemonSqueezy Zapier Triggers:

  • Subscription Created
  • Subscription Cancelled
  • Subscription Expired

These 3 are the ones that will work for us. You can further develop your system by using other options.

First of all, I select the “Subscription Created” option and click the “Continue” button.
Then you need to link your account. After connecting my account, I click the “Continue” button again.
Then I have to choose my store. My store’s name is “Ugur KILCI“.
And again I click the “Continue” button.
When you click the “Test Trigger” button, it will give an error because you do not have a subscriber yet. This is normal.
Click the “Skip test” button.

In Stage 2, we must now choose an action. For action, Google Sheets was the first thing that came to my mind. But I have little knowledge of the NoCode field. That’s why I asked my friend @NocoderEfe, who is an expert on this subject, over the phone. He said it makes more sense to use Airtable instead of Google Sheets. He said he can connect with Softr if he uses Airtable. You can show data inserted in Airtable within a sheet in Softr. This could be your panel page etc. It’s your job next. You can improve your tool on it.

Efe also said, “You can add subscribers to Google Sheets. Then you can create a new Zap and parse the newly added Row and use it for your own purposes.” Efe works at a NoCode agency called KodsuzWeb. They just exit the NoCode area a few days ago. They wrote the book on the nocode business. No kidding, they really wrote the book. NoCode Bible and Become a NoCoder. It mostly uses Bubble, API works and Webhook can already be done with Bubble.

In this article, how can we add a new subscriber (customer) to our tool? I explained how we can show or not show some pages in our panel. Although I explained this with PHP, I explained that it can be done with JS or Python using algorithm logic. I told you that you can do this with nocode using Zapier. Or, I informed you that a direct API connection can be made with Bubble.io. In the article, I told memories from my own Micro SaaS adventure. I told about the problems I experienced in these memories and their solutions. These are problems that only people who have made Micro SaaS can see.

Yes, it has been a long post. 😅 I tried to go into as much detail as possible and present it in the best quality possible. You can leave your feedback as a comment under this blog post. Or you can give feedback by tagging me on Twitter and linking to the blog post.

I share Micro SaaS ideas on Twitter every day. Follow me on Twitter. I would be very happy if you RT my tweets that you like. 🤗

I share my new blog posts, the products I have developed, my achievements, and the lessons I have learned in my newsletter. Be sure to subscribe to my newsletter.

Finally, if you liked this article, could you share it? Thanks. See you in the next article.

Leave a Reply

Your email address will not be published. Required fields are marked *