Overview
These examples show how to integrate AI Pass into PHP applications using the JavaScript SDK for authentication and PHP for server-side API calls.
Live Example: View working PHP demo
Login Page (index.php)
Handle OAuth authentication using the AI Pass SDK:
<?php
$isLoggingOut = isset($_GET['logout']) && $_GET['logout'] === '1';
?>
<!DOCTYPE html>
<html>
<body>
<h1>AI Pass OAuth2 Example</h1>
<div id="login-section">
<button onclick="login()">Login with AI Pass</button>
</div>
<script src="https://aipass.one/aipass-sdk.js"></script>
<script>
AiPass.initialize({
clientId: 'your_client_id',
scopes: ['api:access', 'profile:read']
});
// Handle logout
const isLoggingOut = <?php echo $isLoggingOut ? 'true' : 'false'; ?>;
if (isLoggingOut) {
AiPass.logout().finally(() => {
window.location = 'index.php';
});
}
// Redirect to dashboard after login
document.addEventListener('aipass:login', redirectToDashboard);
if (AiPass.isAuthenticated()) redirectToDashboard();
function redirectToDashboard() {
const token = AiPass.getAccessToken();
const form = document.createElement('form');
form.method = 'POST';
form.action = 'dashboard.php';
const input = document.createElement('input');
input.type = 'hidden';
input.name = 'access_token';
input.value = token;
form.appendChild(input);
document.body.appendChild(form);
form.submit();
}
async function login() {
await AiPass.login();
}
</script>
</body>
</html>
Dashboard (dashboard.php)
Use the access token to call AI Pass APIs from PHP:
Session Management & Logout
<?php
session_start();
// Store token from POST
if (!isset($_SESSION['access_token']) && isset($_POST['access_token'])) {
$_SESSION['access_token'] = $_POST['access_token'];
}
// Redirect if not authenticated
if (!isset($_SESSION['access_token'])) {
header('Location: index.php');
exit();
}
$token = $_SESSION['access_token'];
// Handle logout
if (isset($_POST['logout'])) {
session_destroy();
header('Location: index.php?logout=1');
exit();
}
?>
API Call Helper Function
function callAiPassAPI($endpoint, $method, $token, $data = null) {
$ch = curl_init('https://aipass.one' . $endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $token,
'Content-Type: application/json'
]);
if ($method === 'POST') {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
}
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
Get User Profile
$userInfo = callAiPassAPI('/oauth2/userinfo', 'GET', $token);
$userName = $userInfo['name'] ?? $userInfo['email'] ?? 'Unknown';
$userEmail = $userInfo['email'] ?? '';
Get User Balance
$balance = callAiPassAPI('/api/v1/usage/me/summary', 'GET', $token);
$remainingBudget = $balance['data']['remainingBudget'] ?? 0;
$maxBudget = $balance['data']['maxBudget'] ?? 0;
echo "Balance: $" . number_format($remainingBudget, 2) .
" / $" . number_format($maxBudget, 2);
Call Chat Completion API
if (isset($_POST['prompt']) && !empty($_POST['prompt'])) {
$result = callAiPassAPI('/oauth2/v1/chat/completions', 'POST', $token, [
'model' => 'gemini/gemini-2.5-flash-lite',
'messages' => [
['role' => 'user', 'content' => $_POST['prompt']]
],
'temperature' => 0.7,
'max_tokens' => 1000
]);
$completionResult = $result['choices'][0]['message']['content'] ??
'Error: No response from API';
echo htmlspecialchars($completionResult);
}