<h[% level %] id="[% id %]">
  [% IF uri -%]
  <a href="[% uri %]" rel="collection">
  [% END -%]
  [% title %]
  [% IF uri -%]
  </a>
  [% END -%]
</h[% level %]>

<p>
This documentation describes the Sipwise NGCP HTTP API, which is used to control the NGCP platform via 3rd party
applications. It is also a machine-readable service document and serves as entry point for API clients.
</p>

<p>
The Sipwise NGCP HTTP API strictly follows the REST principle and uses the <a href="http://tools.ietf.org/html/draft-kelly-json-hal-06">JSON Hypertext Application Language (JSON-HAL)</a> to express resources and their relations. This means, compared to typical plain JSON APIs, that relations between resources are defined as hyperlinks within the JSON objects, so a client can and should navigate between related objects without hard-coding any URIs.
</p>

<h[% level + 1%]>Brief Overview of JSON-HAL</h[% level + 1%]>

HAL is a generic media type with which Web APIs can be developed and exposed as series of links. Clients of these APIs can select links by their link relation type and traverse them in order to progress through the application.

A HAL document uses the format described in <a href="http://tools.ietf.org/html/rfc4627">RFC4627 - The application/json Media Type for JavaScript Object Notation (JSON)</a> and has the media type <span>application/hal+json</span>.

An example request to fetch a <i>customercontacts</i> item with id <i>1</i> via the API looks like this:

<code>
   GET /api/customercontacts/1 HTTP/1.1
   Accept: application/hal+json
</code>

The corresponding response is as follows:

<code>
   HTTP/1.1 200 OK
   Content-Type: application/hal+json

   {
     "_links": {
       "self": { "href": "/api/customercontacts/1" },
       "ngcp:reseller": { "href": "/api/resellers/5" },
     },
     "firstname": "John",
     "lastname": "Doe",
     "email": "john.doe@example.org"
   }
</code>

Here, we have a HAL document representing a contact resource with the URI <span>/api/customercontacts/1</span>. It has a link to a <span>reseller</span> it belongs to, and its own state in the form of <span>firstname</span>, <span>lastname</span> and <span>email</span> properties.

To fetch the reseller of this contact, an API client only has to follow the link provided in <span>_links.reseller.href</span>.

A simple code example might look like this:

<code>
  $ua = LWP::UserAgent->new;

  # fetch the contact (URI hardcoded for simplicity only!)
  $contact = from_json($ua->get('https://example.org/api/customercontacts/1'));

  # follow the reseller link to fetch the reseller of this contact
  $reseller = from_json($ua->get($contact->{'_links'}->{'ngcp:reseller'}->{'href'});
</code>

<h[% level + 1%]>API Versioning</h[% level + 1%]>

Due to the JSON-HAL structure, all related resources are hyperlinked, which implies that no strict API versioning is required. If URIs change between NGCP versions, the hyperlinks in the JSON-HAL documents are updated accordingly.

As a consequence, this means that a client implemented against the API <b>should not hardcode URIs</b>, rather than using the hyperlinks provided in the resources.

<h[% level + 1%]>Query Parameters</h[% level + 1%]>


Some collections define query parameters to filter the output. Typical use cases are limiting collections to a specific reseller or a specific customer.

Query parameters are appended to the URL like in the following example:

<code>
curl -X GET 'https://example.org:1443/api/somecollection/?param_one=1&amp;param2=something'
</code>

Some query parameters allow wildcard/pattern matching, which is expressed by a '*' like this:

<code>
curl -X GET 'https://example.org:1443/api/somecollection/?param=*something*'
</code>

<em>Note:</em> this examples do not yet contain authentication and will therefore not work on a standard installation. To read more
about that go to the Chapter <a href="#auth">Authentication</a>.

<h[% level + 1%]>HTTP Response Codes</h[% level + 1%]>

The REST API returns an HTTP response code with the following classes:

<ul>
<li><b>1xx</b>: <em>Provisioninal Response</em> - the client is asked to proceed with the request.</li>
<li><b>2xx</b>: <em>Successful Response</em> - the request has successfully been received and processed.</li>
<li><b>3xx</b>: <em>Redirection Response</em> - the client is asked to contact a different location.</li>
<li><b>4xx</b>: <em>Client Error</em> - the client has provided incorrect information in either the credentials, the location or the data.</li>
<li><b>5xx</b>: <em>Server Error</em> - the server failed to process the request due to an internal error.</li>
</ul>

<h[% level + 2%]>1xx Provisional Responses</h[% level + 2%]>

The server sends the following provisional responses, and the user agent on the client is expected to handle it transparently without any special handling in the code using the API:

<ul>
  <li><b>100 Continue</b>: The client should continue with its request. This interim response is used to inform the client that the initial part of the request has been received and has not yet been rejected by the server. The client should continue by sending the remainder of the request or, if the request has already been completed, ignore this response.</li>
</ul>

<h[% level + 2%]>2xx Successful Responses</h[% level + 2%]>

The server sends the following successful responses:

<ul>
  <li><b>200 OK</b>: The 200 code is sent in response code to a successful GET request on a collection or item, or if a PUT or PATCH request with a <em>Prefer: return=representation</em> header is processed successfully. The body contains the result of the operation.</li>
  <li><b>201 Created</b>: The 201 code is sent in response to a successful POST request creating an item within a collection.</li>
  <li><b>204 No Content</b>: The 204 code is sent in response to a successful PUT or PATCH request with no <em>Prefer</em> header or with <em>Prefer: return=minimal</em> and to a successful DELETE request.</li>
</ul>

<h[% level + 2%]>3xx Redirection Responses</h[% level + 2%]>

The server redirects the clients using the following return codes:

<ul>
  <li><b>301 Moved Permanently</b>: The 301 code is sent in response to a request towards a collection without a trailing slash, e.g. <em>http://example.org:1443/api/something</em> is redirected to <em>http://example.org:1443/api/something/</em>.</li>
</ul>

<h[% level + 2%]>4xx Client Error</h[% level + 2%]>

The server rejects a client request with the following response codes:

<ul>
  <li><b>400 Bad Request</b>: The 400 code is sent in response to a request not validating basic syntactical and semantical rules. These rules include a missing body in a POST, PUT or PATCH request, an invalid body in a PATCH request, invalid operations in a PATCH request, or a malformed body based on the expected content type.</li>
  <li><b>401 Unauthorized</b>: The 401 code is sent in response to a request lacking an <em>Authorization</em> header if no SSL authentication is performed, or an <em>Authorization</em> header with invalid credentials.</li>
  <li><b>403 Forbidden</b>: The 403 code is sent in response to a request trying to access resources which require different privileges (typically a reseller accessing resources of other resellers or of the system), or if you try to create more subscribers than allowed within a customer, or if you try to authenticate with an SSL client certificate with an invalid serial number.</li>
  <li><b>404 Not Found</b>: The 404 code is sent in response to a request trying to access an invalid URL on the system, or if it is trying to perform an operation on an item which does not exist or the user has no access to (e.g. a reseller trying to access items of a different reseller).</li>
  <li><b>415 Unsupported Media Type</b>: The 415 code is sent in response to a PUT, POST or PATCH request which doesn't provide an acceptable <em>Content-Type</em> header. Content types are usually <em>application/json</em> or <em>application/json-patch+json</em> for POST/PUT and PATCH, respectively. However, some resources require different content types like <em>audio/x-wav</em>.</li>
  <li><b>422 Unprocessable Entity</b>: The 422 code is sent in response to a syntactically valid request, which fails to meet semantical conditions, like missing or invalid fields in a JSON structure. The body of the response provides more information about the exact condition that failed.</li>
  <li><b>423 Locked</b>: The 423 code is sent if you try to delete an item which is still in use. This is typically the case when trying to delete system or customer contacts which are still bound to a contract.</li>
</ul>

<h[% level + 2%]>5xx Server Error</h[% level + 2%]>

The server sends the following error codes in case of internal issues:

<ul>
  <li><b>500 Server Internal Error</b>: The 500 code is sent in response to a request which caused an unrecoverable error while processing the request. This is usually caused by database errors when modifying data, and they should never happen. If a 500 is encountered, please open a support ticket with Sipwise, providing the data you passed to the server (URL, headers, body), and the error messages provided in <em>/var/log/ngcp/panel*.log</em> of the server.</li>
</ul>


[% # vim: set tabstop=4 syntax=html expandtab: -%]
