Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.


1.1. Introduction to the NMS Prime API

The NMS Prime API is REST/JSON based. Using the API one can create, modify, delete or inspect database objects. The user input will be validated prior to storing it. If the validation fails a corresponding error message will be returned, informing the user which field failed validation.

A user is authenticated using BasicAuth. Currently only the user with the email address "api@localhost" is allowed to access the API. This user is authorized to create, modify, delete or inspect any database object. In future there will be the role "api" which can be assigned to any user having API access. Furthermore one will be able to grant the various API users different fine-grained access rights for the database objects, just like it is possible for the normal WebGUI.

For most users the most interesting objects will be:

  1. Qos

  2. Contract

  3. Modem

  4. MTA

  5. Telephone Number

By virtue of the HTTP/REST transport protocol many different clients can be used. We will only use curl and python in the upcoming examples.


See OpenAPI for a full API description: https://www.nmsprime.com/api/ 

Drawio
bordertrue
viewerToolbartrue
fitWindowfalse
diagramNameoss-bss
simpleViewerfalse
width600
diagramWidth1020
revision2
title


Info

Please use the email address as username, when trying to authenticate with the API. The password is the same you use to login to the NMS Prime web interface.

1.2. Inspecting database objects

Code Blocklanguagebash
1.2.1.1.1. Inspecting Qos objects using curl
Code Block
collapse
language
true
bash
# GET all Qos objects 
$
(in 
curl
this 
-u
case id 1 and 6)
$ curl -u api@localhost:secret https://localhost:8080/admin/api/v0/Qos
[
{"models":{"1":{"id":1,"created_at":"
2015
2016-
12
09-
16
08 
14
10:
20
38:
25
15","updated_at":"
2018
2020-
06
04-10 15
18
:
05
57:
14
34","deleted_at":null,"ds_rate_max":
100
2,"us_rate_max":
10
1,"ds_rate_max_help":
104857600
2000000,"us_rate_max_help":
10485760
1000000,"name":"
100M"},
2:1","ds_name":"D2M","us_name":"U1M"},"6":{"id":
2
6,"created_at":"
2016
2018-
02
01-
15
11 
14
13:
11
29:
40
00","updated_at":"2018-06-15 18:05:14","deleted_at":null,"ds_rate_max":
25
50,"us_rate_max":2,"ds_rate_max_help":
26214400
50000000,"us_rate_max_help":
2097152
2000000,"name":"
25M
50:2"
}
,
{
"
id
ds_name":
3
"D50M","
created
us_
at
name":"
2016-02-15 14:11:55","updated_at":"2018-06-15 18:05:14","deleted_at":null,"ds_rate_max
U2M"}},"success":true}

# GET Qos object with a certain id (6)
$ curl -u api@localhost:secret https://localhost:8080/admin/api/v0/Qos/6
{"models":{"6":{"id":6,"
us
created_
rate_max
at":
2
"2018-01-11 13:29:00","
ds_rate_max_help":6291456,"us_rate_max_help":2097152,"name":"6M"},{"id":6,"created_at":"2018-01-11 13:29:00","updated_at":"2018-
updated_at":"2018-06-15 18:05:14","deleted_at":null,"ds_rate_max":50,"us_rate_max":2,"ds_rate_max_help":
52428800
50000000,"us_rate_max_help":
2097152
2000000,"name":"50
M
:2"
}
,
{"id":7,"created_at
"ds_name":"
2018-01-26 14:00:36
D50M","
updated
us_
at
name":"
2018-06-15 18:05:14","deleted_at":null,"ds_rate_max":1,"us_rate_max":0.5,"ds_rate_max_help":1048576,"us_rate_max_help":524288,"name":"1 M"}] # GET Qos object with a certain id (6) $ curl -u api@localhost:secret https://localhost:8080/admin/api/v0/Qos/6 {"id":6,"created_at":"2018-01-11 13:29:00","updated_at":"2018-06-15 18:05:14","deleted_at":null,"ds_rate_max":50,"us_rate_max":2,"ds_rate_max_help":52428800,"us_rate_max_help":2097152,"name":"50 M"} Code Blocklanguagepytitle
U2M"}},"success":true,"id":6}


Info

You need to add verify=False in the get/post/patch statements, if you haven't set up a proper TLS certificate yet. You may do so using Getting a X.509 certificate via Let's Encrypt


1.2.1.1.2. Inspecting database objects using python
linenumbers
Code Block
true
language
collapsetrue
py
import json
import requests

# initialize session and authenticate using BasicAuth
session = requests.Session()
session.auth = ('api@localhost', 'secret')

# GET all Qos objects
r = session.get('https://localhost:8080/admin/api/v0/Qos')
# GET Qos object with a certain id (6)
r = session.get('https://localhost:8080/admin/api/v0/Qos/6')

# GET all Contract objects
r = session.get('https://localhost:8080/admin/api/v0/Contract')
# GET Contract object with a certain id (500000)
r = session.get('https://localhost:8080/admin/api/v0/Contract/500000')

# GET all Modem objects
r = session.get('https://localhost:8080/admin/api/v0/Modem')
# GET Modem object with a certain id (100001)
r = session.get('https://localhost:8080/admin/api/v0/Modem/100001')

# GET all Mta objects
r = session.get('https://localhost:8080/admin/api/v0/Mta')
# GET Mta object with a certain id (300001)
r = session.get('https://localhost:8080/admin/api/v0/Mta/300001')

# GET all Phonenumber objects
r = session.get('https://localhost:8080/admin/api/v0/Phonenumber')
# GET Phonenumber object with a certain id (300001)
r = session.get('https://localhost:8080/admin/api/v0/Phonenumber/300001')

# Search using arbitrary model attributes
# returns e.g. cm-100000 (00:11:22:33:44:55) and cm-100001 (66:77:88:99:AA:BB) of contract 500001
r = session.get('https://localhost:8080/admin/api/v0/Modem?contract_id=500001')
# returns only cm-100000 (00:11:22:33:44:55)
r = session.get('https://localhost:8080/admin/api/v0/Modem?contract_id=500001&mac=00:11:22:33:44:55')

print(r.text)

1.3. Creating, modifying and deleting database objects

Code Blocklanguagepytitle
1.3.1.1.1. Creating database objects using python
linenumbers
Code Block
true
language
collapsetrue
py
import json
import requests
session = requests.Session()
session.auth = ('api@localhost', 'secret')

# GET description of all fields of this object, the ones with an asterisk (*) must be provided, otherwise validation will fail
r = session.get('https://localhost:8080/admin/api/v0/Contract/create')

# CREATE a contract
r = session.post('https://localhost:8080/admin/api/v0/Contract', json={'number' : '0123456789', 'firstname' : 'Max', 'lastname' : 'Mustermann', 'street' : 'Dörfelstraße', 'house_number' : '7', 'zip' : '09496', 'city' : 'Marienberg', 'phone' : '037359387570', 'birthday' : '1970-01-01'})
# {"
ret
success":
"success"
true,"id":500001}

# MODIFY first name of newly created contract
ret = json.loads(r.text)
r = session.patch('https://localhost:8080/admin/api/v0/Contract/{}'.format(ret['id']), json={'firstname' : 'Maxi'})
# {"
ret
success":true,"
success
id":500001}

# CREATE a contract with missing birthday -> validation fails
r = session.post('https://localhost:8080/admin/api/v0/Contract', json={'number' : '0123456789', 'firstname' : 'Missing', 'lastname' : 'Birthday', 'street' : 'Dörfelstraße', 'house_number' : '7', 'zip' : '09496', 'city' : 'Marienberg', 'phone' : '037359387570'})
# {"
ret
validations":{"birthday":["The birthday field is required."]},"success":false}

# DELETE a contract with a certain id (500001)
r = session.delete('https://localhost:8080/admin/api/v0/
Modem
Contract/500001')
# {"
ret
success":
"success"languagepytitle
true}


# The same operations are available for all the other database objects as well. e.g.:
# Qos
r = session.get('https://localhost:8080/admin/api/v0/Qos/create')
r = session.post('https://localhost:8080/admin/api/v0/Qos', json={'name' : '1000:100 test', 'ds_rate_max' : '1000', 'us_rate_max' : '100'})
ret = json.loads(r.text)
r = session.patch('https://localhost:8080/admin/api/v0/Qos/{}'.format(ret['id']) json={'name' : '100:10 test', 'ds_rate_max' : '100', 'us_rate_max' : '10'})
r = session.post('https://localhost:8080/admin/api/v0/Qos', json={'name' : 'missing us rate test', 'ds_rate_max' : '1000'})
r = session.delete('https://localhost:8080/admin/api/v0/Modem/{}'.format(ret['id']))

# Modem
r = session.get('https://localhost:8080/admin/api/v0/Modem/create')
r = session.post('https://localhost:8080/admin/api/v0/Modem', json={'mac' : '00:11:22:33:44:55', 'contract_id' : '500000', 'configfile_id' : '3', 'qos_id' : '1'})
ret = json.loads(r.text)
r = session.patch('https://localhost:8080/admin/api/v0/Modem/{}'.format(ret['id']) json={'configfile_id' : '6'})
r = session.delete('https://localhost:8080/admin/api/v0/Modem/{}'.format(ret['id']))

# Mta
r = session.get('https://localhost:8080/admin/api/v0/Mta/create')
r = session.post('https://localhost:8080/admin/api/v0/Mta', json={'mac' : '11:22:33:44:55:66', 'modem_id' : '100000', 'configfile_id' : '186'})
ret = json.loads(r.text)
r = session.patch('https://localhost:8080/admin/api/v0/Mta/{}'.format(ret['id']) json={'configfile_id' : '181'})
r = session.delete('https://localhost:8080/admin/api/v0/Mta/{}'.format(ret['id']))

# Phonenumber
r = session.get('https://localhost:8080/admin/api/v0/Phonenumber/create')
r = session.post('https://localhost:8080/admin/api/v0/Phonenumber', json={'country_code' : '0049', 'prefix_number' : '030', 'number' : '123456', 'mta_id' : '300000', 'port' : '1', 'username' : 'mustermann', 'password' : 'secret', 'sipdomain' : 'sip.provider.com', 'active' : '1'})
ret = json.loads(r.text)
r = session.patch('https://localhost:8080/admin/api/v0/Phonenumber/{}'.format(ret['id']) json={'active' : '0'})
r = session.delete('https://localhost:8080/admin/api/v0/Phonenumber/{}'.format(ret['id']))

print(r.text)
Code Block


1.3.1.1.2. Restarting modems/MTAs and getting the online status of a modem
linenumbers
Code Block
true
language
collapse
py
true
import json
import requests
session = requests.Session()
session.auth = ('api@localhost', 'secret')

r = session.get('https://localhost:8080/admin/api/v0/Mta/300023/restart')
# {"
ret
success":true,"
success
id":300023}
r = session.get('https://localhost:8080/admin/api/v0/Mta/300023/restart')
# {"messages":{"
ret
errors":["Could not restart MTA! (offline?)"]},"success":true,"id":300023}

r = session.get('https://localhost:8080/admin/api/v0/Modem/100003/status')
# {"
ret
success":
"success"
true,"
online
id":
true
100003}
r = session.get('https://localhost:8080/admin/api/v0/Modem/100003/restart')
# {"messages":{"
ret
infos":["Das Modem wurde erfolgreich 
über
\u00fcber das 
CMTS neugestartet"
NetGw neu gestartet"]},"success":true,"id":100003}
r = session.get('https://localhost:8080/admin/api/v0/Modem/100003/status')
# {"
ret
success":
"success"
false,"
online
id":
false
100003}
r = session.get('https://localhost:8080/admin/api/v0/Modem/100003/restart')
# {
"ret
"messages":{"errors":["Das Modem konnte nicht neu 
neugestartet
gestartet werden! (offline?)"]},"success":true,"id":100003}

print(r.text)

1.4. Getting API URLs

In order to modify objects of other types than the ones mentioned above one needs to find the according API URL. This can be accomplished using the WebGUI by clicking on the according object. For example one may want to modify Endpoints. They can be retrieved using the WebGUI by clicking on "Provisioning

" → "Endpoints". This leads to the URL https://localhost:8080/admin/Endpoint. Thus the API Base-URL for the endpoints will be: https://localhost:8080/admin/api/v0/Endpoint.

" → "Endpoints". This leads to the URL https://localhost:8080/admin/Endpoint. Thus the API Base-URL for the endpoints will be: https://localhost:8080/admin/api/v0/Endpoint.


1.4.1. Postman API Demo Collection:

Set the variable "base_url" for the collection and enter your credentials (email and password) into Basic Auth fields.

Run in PostmanImage Added or import the json from here:

View file
nameNMS Prime API Demo.postman_collection.json