Skip to content
Sign Up For Free
BatchKeys License Key SaaS
  • Pricing
  • Try Demo
  • Docs
  • SupportExpand
    • Tickets
  • AboutExpand
    • Contact
  • Dashboard
BatchKeys License Key SaaS

Getting Started

11
  • Setting Up An Account
  • Your Dashboard
  • Generating Keys
  • Viewing And Editing Keys
  • Viewing/Editing An Individual Key’s Details
  • Bulk Deleting Keys
  • Importing Keys
  • Encoding And Decoding PHP
  • Handling Refunds
  • Unbanning A Valid End User
  • Requesting An Enterprise or Custom Plan

Integrations

14
  • Inserting Code Into Your App
  • Making API Calls
  • Receiving A Response
  • Example JSON Response
  • Assigning Keys To A Domain Name
  • Generating Unique UUIDs
  • Assigning Keys To A UUID
  • Checking Keys Daily Using A Cron Job
  • Checking Keys At Startup
  • Consuming And Topping Up Tokens For Token-Based Keys
  • Server-Side Proxy For Client-Side JavaScript Connections
  • Forms To Collect A Key
  • Adding BatchKeys To A WordPress Plugin
  • Adding BatchKeys To A WordPress Theme

Best Practices

13
  • Using Domain/UUID Settings To Upsell Your App
  • Staying Within Rate Limits
  • Handling Errors
  • Dealing With Connection Failures And Rechecks
  • Securing Keys
  • Working Around Hardware Changes With V5 UUIDs
  • Working Around V4 UUID Security Concerns
  • Creating Good Validation Forms
  • How We Use UUIDs To Secure Keys
  • A Note About Example Codes
  • Can We Create Custom Code For You?
  • Maximum Key Limits Per Plan
  • Using A License Key Service In A Mobile App
  • Home
  • Docs
  • Integrations
  • Checking Keys At Startup

Checking Keys At Startup

It’s recommended to add a piece of code that checks the end user’s key every time your app starts. Adding such a code will make issuing refunds much easier and ensure your app stays protected.

NOTE: If your app installs on a website, such as a plugin or theme, you should check the key using a cron job instead.

Getting The Key #

When your app first activates a key, it’s best practice to save the key from the user input into a secure local file or database. From this data you can preform key checks any time you need.

Apps Written In Python #

For Python, you can use the subprocess.call() function to run a key_check file (that contains the actual key checking logic). It then checks the exit code of the process: if it’s 0, the key check was successful; otherwise, it was not, and the program prints an error message and exits.

import subprocess

def main():
    # Run the key_check file
    result = subprocess.call("./key_check")

    # Check if the key_check execution was successful
    if result == 0:
        print("Key check successful.")
        # Continue with the rest of your software's startup process
    else:
        print("Key check failed. Exiting.")
        # Exit the software if the key check fails
        return 1

    # Your software's main code goes here

    return 0

if __name__ == "__main__":
    main()

Apps Written In C #

You can use the system() function to run a file (we called it key_check) every time your software starts up in C. Here’s a basic example of how you might implement this.

#include <stdio.h>
#include <stdlib.h>

int main() {
    // Run the key_check file
    int result = system("./key_check");

    // Check if the key_check execution was successful
    if (result == 0) {
        printf("Key check successful.\n");
        // Continue with the rest of your software's startup process
    } else {
        printf("Key check failed. Exiting.\n");
        // Exit the software if the key check fails
        return 1;
    }

    // Your software's main code goes here

    return 0;
}

Apps Written In C++ #

Here’s anexample of how you might implement a license key check at the start of your C++ application. Again, we have called the actual key checking file key_check. Inside that file will be the actual logic to check the key.

#include <iostream>
#include <cstdlib>

int main() {
    // Run the key_check file
    int result = system("./key_check");

    // Check if the key_check execution was successful
    if (result == 0) {
        std::cout << "Key check successful.\n";
        // Continue with the rest of your software's startup process
    } else {
        std::cout << "Key check failed. Exiting.\n";
        // Exit the software if the key check fails
        return 1;
    }

    // Your software's main code goes here

    return 0;
}

Apps Written In C# #

Here we use the Process class from the System.Diagnostics namespace to start the key_check process. We set up a ProcessStartInfo object to specify the file to run and redirect its output. We then wait for the process to finish using process.WaitForExit() and check its exit code to determine if the key check was successful.

using System;
using System.Diagnostics;

class Program
{
    static void Main()
    {
        // Run the key_check file
        ProcessStartInfo startInfo = new ProcessStartInfo("./key_check");
        startInfo.UseShellExecute = false;
        startInfo.RedirectStandardOutput = true;

        Process process = new Process();
        process.StartInfo = startInfo;
        process.Start();

        // Wait for the process to finish
        process.WaitForExit();

        // Check if the key_check execution was successful
        if (process.ExitCode == 0)
        {
            Console.WriteLine("Key check successful.");
            // Continue with the rest of your software's startup process
        }
        else
        {
            Console.WriteLine("Key check failed. Exiting.");
            // Exit the software if the key check fails
            Environment.Exit(1);
        }

        // Your software's main code goes here
    }
}

A Java Based App #

In this Java version, we use the Runtime.getRuntime().exec() method to start the key_check process. We then wait for the process to finish using process.waitFor() and check its exit code to determine if the key check was successful.

import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        try {
            // Run the key_check file
            Process process = Runtime.getRuntime().exec("./key_check");
            int result = process.waitFor();

            // Check if the key_check execution was successful
            if (result == 0) {
                System.out.println("Key check successful.");
                // Continue with the rest of your software's startup process
            } else {
                System.out.println("Key check failed. Exiting.");
                // Exit the software if the key check fails
                System.exit(1);
            }

            // Your software's main code goes here

        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
}

Example Inside An Unreal Engine Game #

To check a key on startup in Unreal Engine, you can create a custom C++ class that handles the key check process and then call this class from your game’s initialization code.

Create a Key Checker Class: Create a new C++ class in your Unreal Engine project that will handle the key checking process. For example, you can create a class called KeyChecker.

// KeyChecker.h
#pragma once

#include "CoreMinimal.h"

class MYGAME_API KeyChecker
{
public:
    static bool CheckKey();
};
// KeyChecker.cpp
#include "KeyChecker.h"
#include <iostream>
#include <cstdlib>

bool KeyChecker::CheckKey()
{
    // Run the key_check file
    int result = system("./key_check");

    // Check if the key_check execution was successful
    return result == 0;
}

In your game’s initialization code, call the KeyChecker::CheckKey() method to perform the key check. You can do this in the StartPlay() method of your game mode class or any other appropriate location in your game’s startup sequence.

// MyGameMode.cpp
#include "MyGameMode.h"
#include "KeyChecker.h"

void AMyGameMode::StartPlay()
{
    Super::StartPlay();

    if (KeyChecker::CheckKey())
    {
        // Key check successful, continue game initialization
        UE_LOG(LogTemp, Log, TEXT("Key check successful."));
    }
    else
    {
        // Key check failed, terminate the game or take appropriate action
        UE_LOG(LogTemp, Error, TEXT("Key check failed. Exiting."));
        FGenericPlatformMisc::RequestExit(false);
    }
}

Example Inside A Unity Game #

To check a key on startup in a Unity game, you can use the Start() method of a MonoBehaviour script to execute the key checking process. Create a new C# script in your Unity project, for example, KeyCheckManager.cs.

using System.Diagnostics;
using UnityEngine;

public class KeyCheckManager : MonoBehaviour
{
    void Start()
    {
        // Run the key_check file
        ProcessStartInfo startInfo = new ProcessStartInfo("./key_check");
        startInfo.UseShellExecute = false;
        startInfo.RedirectStandardOutput = true;

        Process process = new Process();
        process.StartInfo = startInfo;
        process.Start();

        // Wait for the process to finish
        process.WaitForExit();

        // Check if the key_check execution was successful
        if (process.ExitCode == 0)
        {
            Debug.Log("Key check successful.");
            // Continue with the rest of your game's startup process
        }
        else
        {
            Debug.LogError("Key check failed. Exiting.");
            // Exit the game if the key check fails
            Application.Quit();
        }
    }
}

Next, create an empty GameObject in your Unity scene (e.g., name it “GameManager”) and attach the KeyCheckManager script to it. This script will run automatically when the game starts, checking the key.

Example Inside A GoDot Game #

To run a key_check file on startup in a Godot game, you can use GDScript to execute an OS command. Create a new GDScript in your Godot project, for example, KeyCheck.gd.

extends Node

func _ready():
    # Run the key_check file
    var result = OS.execute("./key_check", [], true)
    
    # Check if the key_check execution was successful
    if result == 0:
        print("Key check successful.")
        # Continue with the rest of your game's startup process
    else:
        print("Key check failed. Exiting.")
        # Exit the game if the key check fails
        get_tree().quit()

Next, create a new Node in your Godot scene (e.g., name it “GameManager”) and attach the KeyCheck.gd script to it. This script will run automatically when the game starts, checking the key.

Was this Helpful?
Still stuck? How can we help?

How can we help?

Updated on April 4, 2024
Checking Keys Daily Using A Cron JobConsuming And Topping Up Tokens For Token-Based Keys

Powered by BetterDocs

Table of Contents
  • Getting The Key
  • Apps Written In Python
  • Apps Written In C
  • Apps Written In C++
  • Apps Written In C#
  • A Java Based App
  • Example Inside An Unreal Engine Game
  • Example Inside A Unity Game
  • Example Inside A GoDot Game
  • Get Early Access
  • Try Demo
  • Status
  • API Tester
  • Docs
  • Support
  • Feature Request
  • Feedback
  • About
  • Contact
  • Sitemap
  • Coupons

TOS

Privacy Policy

SLA

© 2025 Batch Keys | Created and maintained by Kayce Media Group

  • Pricing
  • Try Demo
  • Docs
  • Support
    • Tickets
  • About
    • Contact
  • Dashboard