How to create a unique template link with generateSingleUseLink

Topic
Materials

The curl command as displayed on the ManyChat API Swagger is:

curl -X POST "https://api.manychat.com/user/template/generateSingleUseLink" -H "accept: application/json" -H "Authorization: Bearer 0" -H "Content-Type: application/json" -d "{ \"template_id\": 0}"

If you use cUrl, change (Bearer) 0 with your ManyChat Profile API key, and put your template ID in the command, like \"template_id\": 123456798.

Please note that for the Template API to work, you are NOT using the regular public API key, but your PROFILE API key. You can find that in the following path: My Profile > Settings

The purpose of this API call is to create a one time use link for your template installation.

Why would you need this?

This is great if you do not want other people sharing your templates, with the use of this simple feature, the templates can only be installed one time. When the URL is used, the link is no longer valid.

This is the default response on a successful request.

{
  "status": "success",
  "data": {
    "hash_code": "string",
    "installation_link": "string"
  }
}

You can use the installation link anywhere on your pages, or autoload them in your chatbot after purchase.

Let’s create a unique link

First we will add a new function to our class file in classes/manychat.class.php.

    /**
     * 
     * @param type $template_id
     * @param type $mc_profile_api
     * @return type
     */
    function generateSingleUseLink($template_id, $mc_profile_api)
    {

        $bot_vars            = [
            'template_id' => $template_id
        ];
        $postfields_bot_vars = json_encode($bot_vars, JSON_NUMERIC_CHECK);

        $ch            = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://api.manychat.com/user/template/generateSingleUseLink");
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields_bot_vars . PHP_EOL);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $headers       = [
            'Authorization: Bearer ' . preg_replace("/^(\w+\s+)/", "", $mc_profile_api),
            'Content-Type: application/json',
            'Accept: application/json'
        ];
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $server_output = curl_exec($ch);
        curl_close($ch);

        $json_obj  = json_decode($server_output);
        $good_call = $json_obj->status;

        if ($good_call == "success") {
            return $json_obj->data->installation_link;
        } else {
            return false;
        }
    }

After we created the new function, we will create a new PHP file and call it template_generate_single_use_link.php.

<?php

/*
 * Load the ManyChat Config File
 */
require_once './config.inc.php';

/*
 * Template Generate Single Use Link
 */

$template_id = filter_input(INPUT_GET, 'tpl', FILTER_VALIDATE_INT);

$template_id ?: exit('A template Id is obligatory');
$uniqueLink  = $mc->generateSingleUseLink($template_id, $mc_profile_api);

if ($uniqueLink) {
    echo $uniqueLink;
}else{
    echo "/index.html";
}

The PHP file is very short and simple. All it needs to do is check if there’s a result and publish the new URL.

The URL can be called like template_generate_single_use_link.php?tpl=123456 where 123456 is your template Id. You can find that here:

Let’s call the URL and see what it returns in the browser:

Calling the URL without a querystring, returns an error, we need to append a template Id, let’s retry:

Bingo! As you can see we now get a nice single use template Id.

One more try, with a non existent template Id:

See, it works perfectly fine. You can now use this API to generate a link in your source code by – for example – using it embedded in your hyperlink.

There are a million ways to do this, but I will edit the PHP script a bit, so we can use the URL of that script as the link in the HREF code. The new PHP file looks like this:

<?php

/*
 * Load the ManyChat Config File
 */
require_once './config.inc.php';

/*
 * Template Generate Single Use Link
 */

$template_id = filter_input(INPUT_GET, 'tpl', FILTER_VALIDATE_INT);

$template_id ?: exit('A template Id is obligatory');
$uniqueLink = $mc->generateSingleUseLink($template_id, $mc_profile_api);

if ($uniqueLink) {
    header('Location: ' . $uniqueLink);
    exit;
} else {
    header('Location: /index.html');
    exit;
}

Now you can easily use the link to that script, as the href URL anywhere in your code.

<a href="template_generate_single_use_link.php?tpl=123456" target"_blank">Download Template</a>

When we click the link on the page now, we get sent to either the Template Installation page, or back to /index.html.

That wraps up the Template API, there is only one at this time. I hope it will benefit your ventures.

Prerequisites for this course:

  • basic PHP knowledge or natural born coding skills
  • a server/hosting to run PHP files
  • any text editor, preferrably one with syntax highlighting
Scroll to Top