One perennial question in the FreePBX forum is “How can I add a new extension programmatically?” With the recent improvements of the GraphQL API for FreePBX and PBXact, we thought we could give a more comprehensive answer to this question, and at the same time produce a tutorial for how to use GraphQL from a script, which can serve as a starting point for anyone looking to use any of the ever-expanding list of GraphQL interfaces.
For more information about GraphQL in FreePBX and PBXact, please refer to earlier blog posts GraphQL support in FreePBX 15 and Expanding FreePBX GraphQL APIs, as well as the GraphQL PBX APIs Documentation
In the tutorial we write a “new hire” script to automatically add an extension to your PBX using Python. It makes use of three GraphQL APIs to query the existing extensions, create a new extension, and apply the updated configuration. It takes you through the PBX-side configuration for external applications, some basic sanity-checks using curl, a simplified python script which just does the query, and finally on to the full script for adding the extension. All the details are here: FreePBX GraphQL Provisioning Tutorial
Here’s what the final script looks like in action:
$ python3 addextension.py "William Robert Windsor III" firstname.lastname@example.org apply Requesting authentication token... Querying PBX for existing extension list... Existing extensions:4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 7006 Adding new extension ID 4024 with name="William Robert Windsor III", email@example.com, vmPassword=4024, ouboundCid=2125554024 Result from PBX: Extension has been created Successfully Applying config...