CDR

This page goes over how you can use the CDR API to have CDR data flow back and forth from difuse. We will go over two primary ways of interacting with the CDR programatically:

  1. Pulling Logs
  2. (POST/PUT)ing CDR entries

Pulling Logs

Authentication

You need to have an extension where the "CDR Access" advanced option is enabled, this will allow you to access the API:

image.png

Extension - Allow CDR Access

Once that is done there's a plethora of ways you can communicate with Difuse programmatically:

  1. Directly with a session token
  2. Using query parameters
  3. Basic Authentication

Directly with a session token

This makes sense if you want to get a feel for the API, all you need to do is login to the admin user (only admin users session token is accepted for this method of authentication) and then visit this URL: https://10.254.248.1/pbx/cdr/data, replace 10.254.248.1 with your IP or domain name. You can tweak the page and limit using the page and limit params respectively.

Using query parameters

DANGER
Avoid using API keys in query parameters; it risks exposure. Use the method described after this for better security
curl https://10.254.248.1/pbx/cdr/data?page=1&limit=10&extension=<EXTENSION>&key=<SHA_256_OF_SECRET>

The above URL is what you would need to use to get a response like this:

Please remember that the extension would be a number and NOT a name, and the key would NOT be the extension's secret instead it's the SHA256 hash of that secret.

{
  "data": [
    {
      "db_id": 1,
      "call_id": "1706950109.0",
      "caller_id": "\"4001\" <4001>",
      "source": "4001",
      "destination_context": "sip_internal",
      "destination": "2011",
      "initial_destination": "2011",
      "computed_destination": "",
      "disposition": "ANSWERED",
      "timestamp": "1706950109",
      "date": "2024-02-03 08:48:29",
      "start_time": "2024-02-03 08:48:29",
      "answer_time": "2024-02-03 08:48:29",
      "end_time": "2024-02-03 08:48:36",
      "recording_url": ""
    }
  ],
  "page": "1",
  "limit": "1",
  "total_pages": 15905
}

Using headers

SUCCESS
Using authorization headers is the best way to authenticate yourself for the API

Let's do this example with curl first:

curl -u <extension>:<password-in-sha256> https://10.254.248.1/pbx/cdr/data?page=1&limit=10

Now in Typescript it looks something like this:

const extension: string = "<extension>";
const passwordSha256: string = "<password-in-sha256>";
const url: string = "https://10.254.248.1/pbx/cdr/data?page=1&limit=10";

fetch(url, {
  method: "GET",
  headers: {
    "Authorization": "Basic " + btoa(`${extension}:${passwordSha256}`)
  }
})
  .then((response: Response) => {
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    return response.json();
  })
  .then((data: any) => console.log(data))
  .catch((error: Error) => console.error("Error:", error));

(POST/PUT)ing CDR entries

Now in this section we'll go over how we can get Difuse to POST/PUT CDR entries as they appear in the database. For giving the details of your Server you need to go to /pbx/settings by navigating to PBX > Settings.

You should be able to see this form on the top most section of the Extra Configurations tab:

image.png

PBX - Settings - Extra Configurations - CDR API

Let's go over the options one by one:

  • Enable CDR Log To API: This is the toggle switch, once enabled it will take whatever entries appeared in the database after the last entry was sent and then send it to the given API URL with the given method.
  • API Key: This is if your API needs it (basically adds the token you give here to x-api-key header)
  • Bearer Token: A Bearer Token is an authentication string sent in the Authorization: Bearer header to securely identify users in API requests. If your API needs this, it can be given here.
  • Username/Password: This is for good old basic authentication
  • Method: There's only 2 to choose from POST or PUT, if you don't know which pick POST here.
  • Parameters: Here you can select a wide array of fields that you want to post to your API.
WARNING
The CDR Log Job runs every 60 seconds, so a call that you make will not appear instantly on the other server.