Production-ready, tested code examples for every BetterAuth v2 endpoint in 9 languages. Each example covers the full authentication flow: session initialization, registration, login, profile access, license validation, variables, files, chat, and logout.
| Language | Directory | Tested With | Dependencies |
|---|---|---|---|
| PHP | PHP/ |
PHP 8.x | cURL extension |
| Python | Python/ |
Python 3.8+ | requests |
| C# | CSharp/ |
.NET 6+ | System.Text.Json (built-in) |
| C++ | Cpp/ |
g++ 11+ / MSVC 2022 | libcurl, nlohmann/json |
| Node.js | NodeJS/ |
Node.js 18+ | None (uses built-in https) |
| Java | Java/ |
Java 17+ | Jackson (com.fasterxml.jackson.core:jackson-databind) |
| Go | Go/ |
Go 1.21+ | None (uses built-in net/http) |
| Rust | Rust/ |
Rust 1.75+ | ureq, serde_json (full cargo project) |
| Ruby | Ruby/ |
Ruby 3.0+ | None (uses built-in net/http) |
| cURL | cURL/ |
curl 7.x | bash, python3 (for JSON parsing) |
- Sign up at betterauth.online and create an application.
- Copy your Application Secret from the dashboard.
- Generate a License Key for testing.
- Pick a language, edit the constants at the top of the file:
$APP_SECRET = 'paste-your-secret-here'; $LICENSE_KEY = 'XXXX-XXXX-XXXX-XXXX';
- Run the example:
php PHP/example.php python3 Python/example.py dotnet run --project CSharp ./Cpp/example node NodeJS/example.js javac Java/BetterAuthExample.java && java -cp .:jackson-databind.jar BetterAuthExample go run Go/example.go cd Rust && cargo run ruby Ruby/example.rb bash cURL/example.sh
Every example exercises these 16 endpoints:
| # | Endpoint | Method | Description |
|---|---|---|---|
| 1 | /ping |
GET | Health check |
| 2 | /app/status |
GET | Application status |
| 3 | /auth/init |
POST | Initialize session |
| 4 | /auth/register |
POST | Register new user |
| 5 | /auth/login |
POST | Login with credentials |
| 6 | /auth/license |
POST | Login with license key |
| 7 | /auth/check |
GET | Verify session |
| 8 | /user |
GET | User profile |
| 9 | /user/subscriptions |
GET | List subscriptions |
| 10 | /user/files |
GET | List available files |
| 11 | /variable/{name} |
GET/POST | Read/write variables |
| 12 | /license/validate |
POST | Validate a license key |
| 13 | /log |
POST | Submit log entry |
| 14 | /chat/{channel} |
GET | Get chat messages |
| 15 | /blacklist/check |
GET | Check IP/HWID blacklist |
| 16 | /auth/logout |
POST | Destroy session |
Two headers are used on protected endpoints:
X-App-Secret: <your 64-char application secret>
X-Session-Id: <session id from /auth/init>
Alternative locations (for endpoints that allow it):
app_secretandsession_idin the request bodyapp_secretas a query string parameterAuthorization: Bearer <app_secret>
All API errors follow a consistent structure:
{
"success": false,
"message": "License key has expired",
"error": { "code": "KEY_EXPIRED" },
"timestamp": 1735689600
}Common error codes:
APP_NOT_FOUND— Invalid application secretINVALID_VERSION— Client version mismatchHWID_MISMATCH— Hardware ID doesn't match stored valueKEY_BANNED— License key was revokedUSER_BANNED— User account is suspendedKEY_EXPIRED— License has passed its expiration dateRATE_LIMITED— Too many requests (60/min general, 20/min auth)
Full error code reference: see the BetterAuth API docs.
All examples are tested for syntax correctness before being committed. The C++ example also includes a complete cmake-friendly layout.
The default rate limits are:
/auth/init— 10 requests per IP per 60 seconds/auth/login,/auth/register,/auth/license— 20 requests per IP per 60 seconds- All other endpoints — 60 requests per IP per 60 seconds
For testing, consider using a fresh test application to avoid hitting limits.
For the complete v2 API reference, visit: https://betterauth.online/docs/
BA-Examples/
├── README.md
├── BetterAuth_Logo.png
├── PHP/example.php
├── Python/example.py
├── CSharp/Program.cs
├── Cpp/example.cpp
├── NodeJS/example.js
├── Java/BetterAuthExample.java
├── Go/example.go
├── Ruby/example.rb
├── Rust/
│ ├── Cargo.toml
│ ├── src/main.rs
│ └── README.md
└── cURL/example.sh
MIT — see LICENSE.
- BetterAuth: https://betterauth.online
- Documentation: https://betterauth.online/docs/
- Dashboard: https://betterauth.online/login/
- Status: https://betterauth.online/status/
- Blog: https://betterauth.online/blog/