Secret Variables

So for the past couple days I've been trying to setup a way for my Components to do API calls without exposing the Application api key. I've tried .env variables, either through my own .env file or through pterodactyl's .env, no success. Now I'm trying to do an API call to a php file in my blueprint extension and always 404 file not found, tried multiple file paths, public/private, nothing works for that. Grantend I'm not very experienced with php so it could be wrong. But here's my php file:
<?php

use Pterodactyl\BlueprintFramework\Libraries\ExtensionLibrary\Admin\BlueprintAdminLibrary as BlueprintExtensionLibrary;

class Database
{

private string $application_api_key;
private string $client_api_key;

public function __construct(BlueprintExtensionLibrary $blueprint)
{
$this->application_api_key = $blueprint->dbGet('^#identifier#^', 'config:application_api_key');
$this->client_api_key = $blueprint->dbGet('^#identifier#^', 'config:client_api_key');
}

public function getApplicationApiKey()
{
return $this->application_api_key;
}

public function getClientApiKey()
{
return $this->client_api_key;
}
}

$database = new Database($blueprint);

// Return the API keys as JSON
header('Content-Type: application/json');
echo json_encode([
'application_api_key' => $database->getApplicationApiKey(),
'client_api_key' => $database->getClientApiKey(),
]);
<?php

use Pterodactyl\BlueprintFramework\Libraries\ExtensionLibrary\Admin\BlueprintAdminLibrary as BlueprintExtensionLibrary;

class Database
{

private string $application_api_key;
private string $client_api_key;

public function __construct(BlueprintExtensionLibrary $blueprint)
{
$this->application_api_key = $blueprint->dbGet('^#identifier#^', 'config:application_api_key');
$this->client_api_key = $blueprint->dbGet('^#identifier#^', 'config:client_api_key');
}

public function getApplicationApiKey()
{
return $this->application_api_key;
}

public function getClientApiKey()
{
return $this->client_api_key;
}
}

$database = new Database($blueprint);

// Return the API keys as JSON
header('Content-Type: application/json');
echo json_encode([
'application_api_key' => $database->getApplicationApiKey(),
'client_api_key' => $database->getClientApiKey(),
]);
1 Reply
Yovez
YovezOP2y ago
And I call it using ApiKeys.ts:
import axios, { AxiosResponse } from "axios";

class APIKeys {
private apiUrl: string = "^#componentroot#^/Database.php";

async getDatabaseData(): Promise<{
application_api_key: string;
client_api_key: string;
}> {
try {
const response: AxiosResponse = await axios.get(this.apiUrl);

return response.data;
} catch (error) {
console.error("Error fetching data from the database:", error);
throw error;
}
}
}

export default APIKeys;
import axios, { AxiosResponse } from "axios";

class APIKeys {
private apiUrl: string = "^#componentroot#^/Database.php";

async getDatabaseData(): Promise<{
application_api_key: string;
client_api_key: string;
}> {
try {
const response: AxiosResponse = await axios.get(this.apiUrl);

return response.data;
} catch (error) {
console.error("Error fetching data from the database:", error);
throw error;
}
}
}

export default APIKeys;
In my main component I have this:
async function fetchDataFromDatabase() {
try {
// Create an instance of the APIKeys class
const apiKeys = new APIKeys();

// Use the instance to get the database data
const { application_api_key, client_api_key } =
await apiKeys.getDatabaseData();

// Now you can use these values in your API calls
setApiKey(application_api_key);
setClientApiKey(client_api_key);

// Perform your API calls or other logic using these keys
} catch (error) {
console.error("Error fetching data from the database:", error);
}
}

fetchDataFromDatabase();
async function fetchDataFromDatabase() {
try {
// Create an instance of the APIKeys class
const apiKeys = new APIKeys();

// Use the instance to get the database data
const { application_api_key, client_api_key } =
await apiKeys.getDatabaseData();

// Now you can use these values in your API calls
setApiKey(application_api_key);
setClientApiKey(client_api_key);

// Perform your API calls or other logic using these keys
} catch (error) {
console.error("Error fetching data from the database:", error);
}
}

fetchDataFromDatabase();
I am probably doing something wrong, or missing something simpler. But I've tried the ways I can think of, and can't ever get it working.

Did you find this page helpful?