The Duda Partner API Node.js library provides convenient access to the Duda Partner API from applications writting in server-side Javascript.
View the duda-node
API docs for Node.js. (coming soon)
Check out some video demonstrations covering how to use the library. (coming soon)
Node 12 or higher.
Install the package with:
npm install @dudadev/partner-api --save
# or
yarn add @dudadev/partner-api
The package needs to be configured with your API credentials:
NOTE: There are two ways to set API credentials: via environment variables or values passed into the constructor. Values passed into the constructor take precendence over environment variables.
const { Duda } = require('@dudadev/partner-api');
// uses DUDA_API_USER and DUDA_API_PASS from the environment
const duda = new Duda();
// uses the provided `user` and `pass`
const duda = new Duda({
user: "username",
pass: "password",
});
// then() / catch()
duda.sites.get({
site_name: "a-site-name",
})
.then((site) => console.log(site))
.catch((error) => console.error(error));
Or using ES modules and async
/await
:
// async / await
import { Duda } from '@dudadev/partner-api';
const duda = new Duda();
(async function() {
try {
const site = await duda.sites.get({ site_name: "a-site-name" });
console.log(site);
} catch(error) {
// handle error
}
})();
Import Duda as the default import (not * as Duda
) and instantiate as new Duda()
.
import { Duda, Types } from '@dudadev/partner-api';
const duda = new Duda();
const switchTemplate = async (site : string, template: number) => {
try {
const opts: Types.GetSiteByNamePayload = { site_name: "a-site-name" };
const site: Types.GetSiteByNameResponse = await duda.sites.get(opts);
console.log(site.site_name);
} catch(error) {
// handle error
}
}
Every method returns a chainable promise which can be used instead of a regular callback:
// get a list of available templates
duda.templates.list()
.then((templates) => {
// create a new site from the first template in the returned array
return duda.sites.create({
template_id: templates[0].template_id;
})
.then((site) => {
return duda.accounts.permissions.grantSiteAccess({
account_name: "account-name",
site_name: site.site_name,
permissions: ["EDIT"]
})
.then(() => {
return duda.accounts.authentication.getSSOLink({
account_name: "account-name",
site_name: site.site_name,
target: "EDITOR"
})
})
})
})
.catch((err) => {
// handle error
})
The package can be initialized with several options:
const duda = new Duda({
user: 'api-user',
pass: 'api-pass',
maxNetworkRetries: 2,
env: Duda.Environments.Sandbox, // also, .Direct, .EU
});
Automatic network retries can be enabled with the maxNetworkRetries config option. This will retry requests n times with exponential backoff if they fail due to an intermittent network problem.
const duda = new Duda({
...,
maxNetworkRetries: 2
})