Do It Yourself

Do-it-yourself (DIY) flows allow customers to start at your website and proceed directly into your white labeled Duda portal. The goal is to enable users to start building a website, without need for service providers to enable any type of access for themselves. Customers will then use the Duda editor to build their own website.

Duda can support several different types of flows, such as freemium (where users can access the website builder without paying), paid flows and more. This guide will walk through a freemium style flow, where users will be able to access the website builder without registering for an account.

🚧

Enterprise accounts only

This example uses the Single Sign-on feature, which is only available to Enterprise accounts.

Requirements

To work with this guide, you’ll need to make sure you have the following items ready:

  • Your Duda API credentials
  • Access to the Duda dashboard
  • PHP development environment. Our examples are given in PHP although any language can be used.

📘

The full PHP code from this guide is available here.

1. Create New Site

A website is required to be created before a user can access the platform. When creating a website, you are required to pass a template_id parameter. This lets Duda know which template to start the site from. A default template_id can be provided which the user can change later . In this case, we will start from the ‘Amber Landing Page’ which has a template id of 20077.

curl -X POST -i https://api.duda.co/api/sites/multiscreen/create \
    -u 'APIusername:APIpassword' \
    -H 'Content-Type: application/json' \
    -d '{ "template_id": "20077"}'
{
    "site_name":"28e1182c"
}
<?php 
//Set API user and password
define("API_USER","xxxxxxxxxx");
define("API_PASS","yyyyyyyyyy");

print "Created Site: " . createSite('20077');

//function to create a website
function createSite($template_id) {
    //create array with data    
    $data = array("template_id"=>$template_id);
    
    //turn data into json to pass via cURL
    $data = json_encode($data);

    //Set cURL parameters
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_URL,'https://api.duda.co/api/sites/multiscreen/create');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERPWD, API_USER.':'.API_PASS);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    //execute cURL call and get template_idte data
    $output = curl_exec($ch);
    //check for errors in cURL
    if(curl_errno($ch)) {
        die('Curl error: ' . curl_error($ch));
    }
    //decode result
    $output = json_decode($output);
    //return unique site_name
    return $output->site_name;
}

Learn more about the POST /sites/multiscreen/create API endpoint.

2. Create Customer Account

After creating the website, our next step is creating an account to access the site. Accounts represent the customer who owns the site. When creating an account the only required field is account_name. This is a unique identifier across your Duda account. It can be an email address, but doesn't have to.

Since we might not know a real identifier for this customer, we use a random string as the account_name. This can be changed later. This ensures that the user we are creating is unique among others in the account.

curl -S -u 'APIusername:APIpassword' -H 'Content-Type: application/json' \
-X POST -i -k https://api.duda.co/api/accounts/create \
-d '{"account_name":"tmp_5903a16748c8c_1493410151"}'
<?php 
//Set API user and password
define("API_USER","xxxxxxxxxx");
define("API_PASS","yyyyyyyyyy");

$date = new DateTime();
$tempAcctName = 'tmp_' . uniqid() . '_' . $date->getTimestamp();

$createdAccount = createCustomerAccount($tempAcctName)

print "Created account: " . $createdAccount;

//function to create a website
function createCustomerAccount($account) {
    $data = array("account_name"=>$account);

    $data = json_encode($data);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_URL,'https://api.duda.co/api/accounts/create');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERPWD, API_USER.':'.API_PASS);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    //execute cURL call
    $output = curl_exec($ch);

    //check result
    if(curl_getinfo($ch,CURLINFO_HTTP_CODE) == 204) {
        curl_close($ch);
        return $account;
    } else {
        curl_close($ch);
        die('Account creation failed, error: '. $output . '');
    }
}

Learn more about the POST /accounts/create API endpoint.

3. Grant Access

After we’ve created a website and account, we need to give the customer access to the newly created website. As part of the granting access, we need to set which permissions the user will have access to. You can use this feature to decide which parts of the platform a user should have access to. In the example below, we exclude Developer Mode, eCommerce and Backups from the customer.

curl -S -u 'APIusername:APIpassword' \
-H 'Content-Type: application/json' \
-X POST \
-k https://api.duda.co/api/accounts/tmp_5903a16748c8c_1493410151/sites/28e1182c/permissions \
-d '{"permissions":["PUSH_NOTIFICATIONS","REPUBLISH","EDIT","INSITE","PUBLISH","CUSTOM_DOMAIN","RESET","SEO","STATS_TAB","BLOG"]}'
<?php 
//Set API user and password
define("API_USER","xxxxxxxxxx");
define("API_PASS","yyyyyyyyyy");

//check if grant access is a success
if (grantAccountAccess('tmp_5903a16748c8c_1493410151','28e1182c')) {
    echo "successfully granted access";
}

function grantAccountAccess($account,$siteName) {

    $permissions = '{"permissions":["PUSH_NOTIFICATIONS","REPUBLISH","EDIT","INSITE","PUBLISH","CUSTOM_DOMAIN","RESET","SEO","STATS_TAB","BLOG"]}';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    //format URL to grant access to email and sitename passed
    curl_setopt($ch, CURLOPT_URL, 'https://api.duda.co/api/accounts/'.$account.'/sites/'.$siteName.'/permissions');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERPWD, API_USER.':'.API_PASS);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $permissions);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    //execute cURL call
    $output = curl_exec($ch);
    //check result for correct HTTP code
    if(curl_getinfo($ch,CURLINFO_HTTP_CODE) == 204) {
        curl_close($ch);
        return true;
    } else {
        curl_close($ch);
        die('Granting access failed, error: '. $output . '<br/>');
    }
}

Learn more about the POST /accounts/{account_name}/sites/{site_name}/permissions API endpoint.

4. Log Customer into Editor

Now that the customer account has access to the new site, we can get the user started. We will generate a SSO link and redirect the user to this link which will send them into the Duda editor. This will allow us to send the user to the template selection page and get started.

curl -S -u 'APIusername:APIpassword' \ 
-H 'Content-Type: application/json' \
-X GET \
-k https://api.duda.co/api/accounts/sso/tmp_5903a16748c8c_1493410151/link?target=RESET_SITE&site_name=28e1182c
{  
  "url": "http://example.mobilewebsiteserver.com/home/site/146856ab?dm_sso=2!eyJyZXFVdWlkIjoiYzU0Y2E0MzYwNDA2NDgwZDlmZmM2MTIxY2FiOTM2MzAifQ"
}
<?php 
//Set API user and password
define("API_USER","xxxxxxxxxx");
define("API_PASS","yyyyyyyyyy");

echo getSSOLink('tmp_5903a16748c8c_1493410151','28e1182c','RESET_SITE');  

function getSSOLink($account,$siteName,$target) {

    $SSOAPIURL = 'https://api.duda.co/api/accounts/sso/' . $account . '/link';
    if($target) {
        $SSOAPIURL .= '?target=' . $target . '&site_name=' . $siteName;
    }

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    //format URL to grant access to email and sitename passed
    curl_setopt($ch, CURLOPT_URL, $SSOAPIURL);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERPWD, API_USER.':'.API_PASS);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    //execute cURL call
    $output = curl_exec($ch);
    
    //check result for correct HTTP code
    if(curl_getinfo($ch,CURLINFO_HTTP_CODE) == 200) {
        curl_close($ch);
        //decode JSON return
        $output = json_decode($output);

        //append 'asNew' URL param to change template select page messaging slightly
        return $output->url . '&asNew=true';
    } else {
        curl_close($ch);
        die('Error getting SSO link: '. $output . '<br/>');
    }
}

Learn more about the GET /accounts/sso/{account_name}/link API endpoint.

📘

SSO - Target

In our example we provide a target of RESET_SITE when generating the SSO link. This sends the user to the template selection page. If the template_id provided in Step 1 should not be changed, then the target parameter can be removed to send the user directly to the editor.

Updated 9 months ago

Do It Yourself


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.