How to Use eADM API Keys
This article explains how to use an API key set to connect to and make calls against the eADM API. The code examples are provided in C# and PowerShell.
Prerequisites
Before you begin, ensure that:
API access is enabled for your organization.
An API key set has been created with the necessary permissions for the endpoints you plan to use.
API fundamentals
The base URI for all API calls is: https://api.eadm.no/service.asmx
You can view all available endpoints and their required parameters by navigating to the base URI in your browser. You can also test most endpoints directly from this page.
Note: Authentication tokens are valid for 8 hours. We recommend wrapping the token retrieval process in a try-catch block to handle expired tokens gracefully.
Authentication workflow
Both of the following examples demonstrate the same two-step process:
Call the
apilogin
endpoint with the API username and password to retrieve an authentication token.Call the
getruleset
endpoint using the token from the previous step to fetch a specific ruleset as a JSON object.
Code examples
PowerShell example
This script authenticates to the API, retrieves a token, and then uses that token to fetch a specific ruleset.
PowerShell
$baseUrl = "https://api.eadm.no/service.asmx"
$username = "apiapi" # Your API username
$password = Read-Host -Prompt "Enter a password"
$orgnr = "NOXXXXXXXXX" # Your organization number
# 1. Authenticate and get a token
$loginUrl = "$($baseURL)/apilogin"
$loginBody = @{
orgnr = $orgnr
username = $username
password = $password
}
$loginResponse = Invoke-RestMethod -Method 'Post' -Uri $loginUrl -Body $loginBody
$token = $null
if ($loginResponse.data[0]) {
$token = $loginResponse.data[0]
}
else {
Write-Host "Login failed. Check the audit log for ID: $($loginResponse.lasterror)"
return
}
# 2. Fetch a ruleset using the token
$rulesetUrl = "$($baseURL)/getruleset"
$rulesetBody = @{
token = $token
id = "36477" # The ID of the ruleset to fetch
orgnr = $orgnr
}
$rulesetResponse = Invoke-RestMethod -Method 'Post' -Uri $rulesetUrl -Body $rulesetBody
$objects = $rulesetResponse.data
# Process the $objects variable which now contains the ruleset data
C# example
This C# console application demonstrates the same workflow: authenticating to get a token and then using the token to fetch a ruleset.
C#
using System;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
// DataContract for deserializing the JSON response
[DataContract]
public class JData
{
[DataMember]
public string lasterror { get; set; }
[DataMember]
public Object[] data { get; set; }
// Other potential response fields
public string queryid { get; set; }
public DateTime exportstart { get; set; }
public DateTime exportend { get; set; }
public int initatedby { get; set; }
public string returncode { get; set; }
public int pagenx { get; set; }
public int resultsprpage { get; set; }
public int pages { get; set; }
public int totalresults { get; set; }
public string[] sortablecolumns { get; set; }
public string sortedby { get; set; }
public bool sortascending { get; set; }
public string[] searchablecolumns { get; set; }
}
class Program
{
static void Main(string[] args)
{
string orgNr = "NOXXXXXXXXX"; // Your organization number
string userName = "api-user"; // Your API username
string password = "your-password"; // Your API password
string apiUrl = "https://api.eadm.no/service.asmx";
int ruleSetIdToFetch = 40718;
try
{
string authToken = GetAuthToken(orgNr, userName, password, apiUrl);
if (!string.IsNullOrEmpty(authToken))
{
Console.WriteLine("Authentication successful.");
GetRuleSet(authToken, orgNr, ruleSetIdToFetch, apiUrl);
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
// Generic method to deserialize JSON
public static T JsonDeserialize<T>(string jsonString)
{
var ser = new DataContractJsonSerializer(typeof(T));
var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
T obj = (T)ser.ReadObject(ms);
return obj;
}
// Method to get the authentication token
private static string GetAuthToken(string orgNr, string userName, string password, string apiUrl)
{
using (var client = new WebClient())
{
var values = new NameValueCollection();
values["orgnr"] = orgNr;
values["username"] = userName;
values["password"] = password;
var response = client.UploadValues(apiUrl + @"/apilogin", values);
var responseString = Encoding.Default.GetString(response);
JData jresp = JsonDeserialize<JData>(responseString);
if (string.IsNullOrEmpty(jresp.lasterror))
{
return jresp.data[0].ToString();
}
else
{
throw new Exception("Authentication failed: " + jresp.lasterror);
}
}
}
// Method to get a specific ruleset
public static void GetRuleSet(string token, string orgNr, int ruleSetId, string apiUrl)
{
using (var client = new WebClient())
{
var values = new NameValueCollection();
values["token"] = token;
values["orgnr"] = orgNr.ToUpper();
values["id"] = ruleSetId.ToString();
var response = client.UploadValues(apiUrl + @"/getruleset", values);
var responseString = Encoding.Default.GetString(response);
JData jresp = JsonDeserialize<JData>(responseString);
if (!string.IsNullOrEmpty(jresp.lasterror))
{
throw new Exception("Failed to get ruleset: " + jresp.lasterror);
}
else
{
Console.WriteLine("Result OK");
Console.WriteLine(responseString);
}
}
}
}