Skip to navigation

C guide

This page walks through the whole C SDK in the order you would use it. Each step starts with the shortest code that works.

Download the C SDK if you have not done so yet.

Requirements

SupportedRecommended
CC99, C11, C17, or C++11 and newer
CompilersAny C99 compiler. Tested with MSVC.The current Visual Studio
WindowsWindows 10 and newer
LibrariesNone on Windows. libcurl and pthreads on Linux and macOS.

Ed25519, SHA-256, SHA-512, PBKDF2 and JSON are built in, so there is no OpenSSL dependency. On Windows, HTTPS uses WinHTTP and the Windows certificate store.

On Linux and macOS, pass your own system_id and store when you create the client, as shown in Options.

Add the SDK to your project

The whole SDK is the single file authlyx.h. Copy it into your project. In exactly one .c file, define AUTHLYX_IMPLEMENTATION before including it:

#define AUTHLYX_IMPLEMENTATION
#include "authlyx.h"

Every other file that uses the SDK includes authlyx.h without the define. With MSVC the Windows libraries are linked automatically. With other compilers, link winhttp bcrypt crypt32 advapi32 user32 shell32 on Windows, or curl pthread m on Linux and macOS.

The example on GitHub also has a CMake project. Add its folder and link the authlyx target:

add_subdirectory(AuthlyX-C-Example)
target_link_libraries(your-app PRIVATE authlyx)

To use libcurl on Windows instead of WinHTTP, define AUTHLYX_USE_CURL in the file with AUTHLYX_IMPLEMENTATION, or turn on the AUTHLYX_USE_CURL CMake option.

Create the client

authlyx_result error;
authlyx_client *app = authlyx_create("YOUR_APP_KEY", "1.0.0", NULL, &error);
if (!app) {
printf("%s\n", error.message);
}
authlyx_result_free(&error);

The first value is your app’s AppKey. Find it in the dashboard under your application’s credentials. The second is the version of the program you are building. AuthlyX compares it with the versions you allow in the dashboard, so raise it each time you ship a new build. See Allowed Versions.

Create one client for the whole program and free it with authlyx_destroy when the program exits.

How calls work

Every call takes the client, its own values and a pointer to an authlyx_result. It waits for the reply, fills the result and returns 1 on success or 0 on failure:

authlyx_result result;
if (authlyx_init(app, &result))
printf("Connected\n");
else
printf("%s: %s\n", result.code, result.message);
authlyx_result_free(&result);

Always free a result with authlyx_result_free before you reuse it. Pass NULL instead of a result when you only need the return value.

A client can be used from several threads. Calls are processed one at a time.

Start the SDK

authlyx_result started;
if (!authlyx_init(app, &started)) {
printf("%s\n", started.message);
} else if (strcmp(started.code, "OFFLINE_MODE") == 0) {
printf("%s\n", started.message);
}
authlyx_result_free(&started);

authlyx_init connects to AuthlyX, checks that this version is allowed and loads your app’s settings. It must succeed before anything else.

CodeSuccessMeaning
APP_INITIALIZEDYesConnected. Continue to sign-in.
OFFLINE_MODEYesNo internet, but this PC has saved offline access.
NETWORK_...NoNo internet and nothing saved. Offer to try again.
UPDATE_REQUIREDNoThis version is older than the one set in your dashboard.
VERSION_MISMATCHNoThis version is newer than the dashboard version and is not allowed.

Sign in

authlyx_result result;
authlyx_license_login(app, "YOUR_LICENSE_KEY", &result);
authlyx_result_free(&result);
authlyx_login(app, "YOUR_USERNAME", "YOUR_PASSWORD", &result);
authlyx_result_free(&result);
authlyx_device_login(app, &result);
authlyx_result_free(&result);

These are three ways to sign in. Use the one that matches the access type you created in the dashboard. Passing NULL as the password to authlyx_login signs in with a license key. Device login signs in the current Windows user with no form, and only works for devices you approved first; see Device login.

Create an account, change the password, add time

authlyx_register(app, "YOUR_USERNAME", "YOUR_PASSWORD", "YOUR_LICENSE_KEY", &result);
authlyx_result_free(&result);
authlyx_change_password(app, "CURRENT_PASSWORD", "NEW_PASSWORD", &result);
authlyx_result_free(&result);
authlyx_redeem_license(app, "NEW_LICENSE_KEY", &result);
authlyx_result_free(&result);

authlyx_register creates the account with a license and signs it in. authlyx_redeem_license adds a license’s time to the signed-in account.

Read the account

char *username = authlyx_user_string(app, "Username");
char *plan = authlyx_user_string(app, "Plan");
char *expiry = authlyx_user_string(app, "ExpiryDate");
printf("%s, %s, %s\n", username ? username : "-", plan ? plan : "-", expiry ? expiry : "-");
authlyx_free(username);
authlyx_free(plan);
authlyx_free(expiry);

authlyx_user_string returns a copy of one field as text, or NULL when it is missing. Free it with authlyx_free. The fields are listed in User Data.

authlyx_user_data returns a copy of every field as a value, which you free with authlyx_value_free:

authlyx_value *user = authlyx_user_data(app);
const authlyx_value *features = authlyx_value_get(user, "features");
size_t index;
for (index = 0; index < authlyx_value_size(features); ++index)
printf("%s\n", authlyx_value_string(authlyx_value_at(features, index)));
authlyx_value_free(user);

Reading values

Data from AuthlyX, such as result.data and authlyx_user_data, is an authlyx_value. These functions read it:

FunctionReturns
authlyx_value_typeAUTHLYX_NULL, AUTHLYX_BOOL, AUTHLYX_NUMBER, AUTHLYX_STRING, AUTHLYX_ARRAY or AUTHLYX_OBJECT
authlyx_value_getA field of an object, or NULL
authlyx_value_at, authlyx_value_key, authlyx_value_sizeItems of an array, or fields and names of an object
authlyx_value_string, authlyx_value_number, authlyx_value_boolA single value, or NULL, 0 and 0 for the wrong type
authlyx_value_textAny value as text, which you free with authlyx_free
authlyx_value_jsonAny value as JSON, which you free with authlyx_free

Every reader accepts NULL, so you can chain them without checking each step. Values read from a result stay valid until you free that result.

Features and metadata

Features come from the plan and the account. Some are on/off, some carry a value such as "high" or 90. Metadata is extra information attached to accounts. See Features and metadata.

authlyx_result result;
if (authlyx_check_feature(app, "exportPdf", &result)) {
char *text = authlyx_value_text(result.data);
printf("%s\n", text ? text : "");
authlyx_free(text);
}
authlyx_result_free(&result);

authlyx_get_features lists every feature and authlyx_get_feature returns one. For metadata, use authlyx_get_all_metadata, authlyx_get_metadata and authlyx_check_metadata.

Save metadata

authlyx_set_metadata(app, "display_name", "Alex", &result);
authlyx_result_free(&result);
authlyx_set_metadata_json(app, "launch_count", "12", &result);
authlyx_result_free(&result);

authlyx_set_metadata saves text. authlyx_set_metadata_json saves a number, a list or an object written as JSON.

Cloud variables

if (authlyx_get_variable(app, "download_url", &result))
printf("%s\n", authlyx_value_string(result.data));
authlyx_result_free(&result);
authlyx_update_variable(app, "maintenance", "false", &result);
authlyx_result_free(&result);

For authlyx_get_variable, data is the value itself. See Cloud variables.

Scripts

authlyx_get_script(app, "welcome_message", -1, &result);
authlyx_result_free(&result);

The third value is the revision. -1 asks for the current one. The SDK checks the script’s signature before returning it. It does not run the script. See Scripts.

Logs and webhooks

authlyx_log(app, "User opened settings", &result);
authlyx_result_free(&result);
authlyx_send_webhook(app, "setup.completed", "{\"step\":3}", &result);
authlyx_result_free(&result);

Webhook data is passed as JSON text. See Logs and Webhooks.

Staying signed in

After sign-in the SDK checks in with AuthlyX on a background thread (a heartbeat) at the interval the server sets, and refreshes features and metadata each time. If you ban, pause or expire the account, the next heartbeat ends the session.

if (!authlyx_has_access(app))
show_login_screen();

To be told when the session ends, set the callbacks before you create the client:

static void access_ended(void *context, const authlyx_result *reason)
{
(void)context;
printf("Signed out: %s\n", reason->message);
}
static void connection_changed(void *context, const char *state)
{
(void)context;
printf("Connection: %s\n", state);
}
authlyx_options options;
authlyx_options_init(&options);
options.on_session_invalidated = access_ended;
options.on_connection = connection_changed;
app = authlyx_create("YOUR_APP_KEY", "1.0.0", &options, &error);

on_connection receives online, disconnected, untrusted or disposed. callback_context is passed to both callbacks. Callbacks can run on the background thread, so keep them short.

To check right away, call authlyx_check_session, for example when your app wakes from sleep. To run the heartbeat yourself, set options.heartbeat = AUTHLYX_HEARTBEAT_MANUAL and call authlyx_check_session from your own timer. More in Sessions and heartbeats.

Offline access

authlyx_license_login(app, license_key, &result);
authlyx_result_free(&result);
authlyx_save_offline_access(app, &result);
authlyx_result_free(&result);

Later, without internet, authlyx_init succeeds with OFFLINE_MODE, and the user signs in with the same license key or username and password. The SDK checks them against the saved permit and the sign-in succeeds with the code OFFLINE_ACCESS. authlyx_has_offline_access tells you whether this PC has a valid saved permit. Signing out deletes it. See Offline access.

Updates

When this version is not allowed, authlyx_init fails with UPDATE_REQUIRED or VERSION_MISMATCH and the SDK shows a message box with the new version, release notes and a button to open your download page. To show your own window, set options.update_prompt = 0 and read the details:

if (!authlyx_init(app, &started) && strcmp(started.code, "UPDATE_REQUIRED") == 0) {
const authlyx_value *update = authlyx_value_get(started.data, "update");
printf("%s %s\n", authlyx_value_string(authlyx_value_get(update, "version")),
authlyx_value_string(authlyx_value_get(update, "url")));
}
authlyx_result_free(&started);

See Automatic updates.

Sign out and shut down

authlyx_logout(app, &result);
authlyx_result_free(&result);
authlyx_destroy(app);

authlyx_logout ends the session, clears the account and deletes saved offline access on this PC. authlyx_destroy stops the background thread and frees the client; call it when your program exits.

GUI apps

Every call waits for the reply. In a GUI, make calls from a worker thread, not the UI thread, or the window stops responding while AuthlyX answers. Pass the result back to the UI thread with your framework’s message mechanism, such as PostMessage in a Win32 app.

Results and errors

FieldMeaning
success1 or 0, the same as the return value
codeA stable code such as APP_INITIALIZED or INVALID_CREDENTIALS
messageA message you can show to the user
kindFor failures: server, network, security, storage or client
statusThe HTTP status, when there was one
request_idThe server request ID, for support
dataThe response data, read with the authlyx_value_* functions

network means the SDK could not reach AuthlyX or confirm the answer; do not treat it as a ban. security means a reply failed its signature checks. client means a value you passed was not valid. All codes are listed in Errors.

Options

Start from authlyx_options_init and change what you need. Every option is optional on Windows.

OptionDefaultMeaning
system_idWindows user SIDRequired on Linux and macOS
storeWindows protected storeWhere the installation key and offline access are saved. Required on Linux and macOS.
request_timeout8.0Seconds allowed for each call, including retries
retry_limit2Retries for temporary network and server errors, from 0 to 5
heartbeatAUTHLYX_HEARTBEAT_SERVERAUTHLYX_HEARTBEAT_MANUAL leaves session checks to you
update_prompt1Shows the Windows update box
ca_bundlenoneA CA bundle file for HTTPS with libcurl
on_connectionnoneCalled with the connection state
on_session_invalidatednoneCalled with the result that ended the session
callback_contextnonePassed to both callbacks

On Linux, for example:

authlyx_options options;
authlyx_options_init(&options);
options.system_id = machine_id;
options.store = authlyx_file_store_create("/var/lib/your-app/authlyx");
app = authlyx_create("YOUR_APP_KEY", "1.0.0", &options, &error);

authlyx_file_store_create writes files readable only by the current user. authlyx_memory_store_create keeps nothing after the process ends. authlyx_protected_store_create is the Windows protected store in a folder you choose. A store you create is yours to free with authlyx_store_destroy after the client is destroyed. You can also fill an authlyx_store with your own load, save and remove functions.

A complete program

#define AUTHLYX_IMPLEMENTATION
#include "authlyx.h"
#include <stdio.h>
#include <string.h>
static void read_line(const char *prompt, char *buffer, size_t size)
{
printf("%s", prompt);
fflush(stdout);
if (!fgets(buffer, (int)size, stdin))
buffer[0] = '\0';
buffer[strcspn(buffer, "\r\n")] = '\0';
}
int main(void)
{
char license_key[128];
char *plan;
authlyx_result result;
authlyx_client *app = authlyx_create("YOUR_APP_KEY", "1.0.0", NULL, &result);
if (!app) {
printf("%s\n", result.message);
authlyx_result_free(&result);
return 1;
}
authlyx_result_free(&result);
if (!authlyx_init(app, &result)) {
printf("%s\n", result.message);
authlyx_result_free(&result);
authlyx_destroy(app);
return 1;
}
authlyx_result_free(&result);
read_line("License key: ", license_key, sizeof license_key);
if (!authlyx_license_login(app, license_key, &result)) {
printf("%s\n", result.message);
authlyx_result_free(&result);
authlyx_destroy(app);
return 1;
}
authlyx_result_free(&result);
plan = authlyx_user_string(app, "Plan");
printf("Signed in. Plan: %s\n", plan ? plan : "-");
authlyx_free(plan);
authlyx_logout(app, NULL);
authlyx_destroy(app);
return 0;
}

The example on GitHub is a fuller console app with a menu for every feature. Each method also has its own page with every language side by side under SDK methods.