WebFaction
Community site: login faq

Hi, can anyone show me what a script would look like to add a sub-domain to my account via the API?

Basically I think the steps are to:

1) first login:

import xmlrpclib

server = xmlrpclib.ServerProxy('https://api.webfaction.com/') session_id, account = server.login('loginformywebfactioncom', 'pw')

2) create the "sub-domain" entry for the sub-domain (I have set the "domain" up already)

server.create_domain(session_id, 'mydomain.com', 'subdomain')

3) but how do I do the equivalent of clicking on the sub-domain entry at https://my.webfaction.com/domains and selecting "Webfaction Hosting -> Normal web traffic (http) is handled by the website: MyAppWebsite"

is it done via the UPDATE website command?

server.update_website(session_id, 'MyAppWebsite', '108.59.could.change', False, ['subdomain.mydomain.com',], ('whatdoiputhere', '/andhere')))

If I do "update" then do I have to list ALL the existing sub-domains or will and update ADD the new one to the existing list? How can I query an existing domain to get the current IP address and the "Site Apps" parameters?
Is that list_websites?

4) Once I get the sub-domain added as a domain and the sub-domain associated with an existing "website" I need to add the sub-domain name to DNS.

Is that with the create_dns_OVERRIDE funtion?
why is this called override, is it going to get reset at some point?

Would it look like:

create_dns_override(session_id, domain, a_ip='', cname='', mx_name='', mx_priority='', spf_record='') May I leave the mail stuff blank?

An example script that does this would be really helpful!

Fred Radford

Skype: NinjaMyApp

asked 04 Jul '12, 11:12

NinjaMyApp
111
accept rate: 0%

Ok, I get the DNS thing now. Basically as long as I use your nameservers, adding the website will create A records for the sub-domains, so I don't need to "override" the DNS settings.

Correct?

(04 Jul '12, 14:07) NinjaMyApp

Correct, NS servers are 'master' servers which assign A, MX, and other kinds of records, creating t he web-site record or DNS override objects in the control panel automatically issue the commands needed to our NS servers to do this.

(04 Jul '12, 17:11) johns

Regarding #3, yes, it is done with the update_website method. You must supply all of the information for the site with each update (all domains/subdomains, all site apps and URL paths, etc). The list_websites API method will list all of your sites' information, so you can get your info from there.

Regarding #4, you do not need to create a DNS override. Adding the domain or subdomain to a site will trigger the automatic creation of the necessary DNS records.

Hope that helps!

permanent link

answered 04 Jul '12, 17:03

seanf
12.2k42136
accept rate: 37%

Based on the above answers, I have #1 and #2 correct, but is there a script for #3? (add a sub-domain via list & update)?

import xmlrpclib

server = xmlrpclib.ServerProxy('https://api.webfaction.com/')
session_id, account = server.login('loginformywebfactioncom', 'pw')

server.create_domain(session_id, 'mydomain.com', 'subdomain')

 #3 GOES HERE
(05 Jul '12, 17:27) NinjaMyApp

Obviously, you need to know the name of the site you want to update, so:

site_to_edit = 'foo' # name of the site you want to update
subdomains_to_add = ['domain.com', 'www.domain.com']

sites = server.list_websites(session_id)
site = [s for s in sites if s['name'] == site_to_edit][0]
site['subdomains'].extend(subdomains_to_add)
server.update_website(
    session_id,
    site['name'],
    site['ip'],
    site['https'],
    site['subdomains'],
    *site['website_apps']
)
(05 Jul '12, 20:08) seanf

Is this then also the adviced way to do dynamic subdomains? Add the subdomain through the API?

(26 Jul '13, 06:31) nathanvda

If you need to create subdomains on demand, then the API is the way to do it.

(26 Jul '13, 12:19) seanf

how can i do this steps in php ?

site = [s for s in sites if s['name'] == site_to_edit][0]
site['subdomains'].extend(subdomains_to_add)
(25 Dec '14, 11:50) Ramy Faiad

The equivalent code in PHP (assuming you're dealing with arrays) would be something like this:

$site = array_filter($sites, function($el) { return $el['name']==$GLOBALS['site_to_edit']; })[0];
$site = array_merge($site['subdomains'], $subdomains_to_add);
(26 Dec '14, 00:03) seanf
showing 5 of 6 show 1 more comments
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×76
×4
×2
×2
×1

question asked: 04 Jul '12, 11:12

question was seen: 4,797 times

last updated: 26 Dec '14, 00:04

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2020 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM