Skip to content

localhost8083/BA-Examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BA-Examples — BetterAuth API v2 Code Examples

BetterAuth License

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.

📚 Languages

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)

🚀 Quick Start

  1. Sign up at betterauth.online and create an application.
  2. Copy your Application Secret from the dashboard.
  3. Generate a License Key for testing.
  4. Pick a language, edit the constants at the top of the file:
    $APP_SECRET  = 'paste-your-secret-here';
    $LICENSE_KEY = 'XXXX-XXXX-XXXX-XXXX';
  5. 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

🔌 Endpoints Covered

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

🔐 Authentication

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_secret and session_id in the request body
  • app_secret as a query string parameter
  • Authorization: Bearer <app_secret>

📝 Error Handling

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 secret
  • INVALID_VERSION — Client version mismatch
  • HWID_MISMATCH — Hardware ID doesn't match stored value
  • KEY_BANNED — License key was revoked
  • USER_BANNED — User account is suspended
  • KEY_EXPIRED — License has passed its expiration date
  • RATE_LIMITED — Too many requests (60/min general, 20/min auth)

Full error code reference: see the BetterAuth API docs.

🧪 Testing

All examples are tested for syntax correctness before being committed. The C++ example also includes a complete cmake-friendly layout.

Rate Limits

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.

📖 Full API Reference

For the complete v2 API reference, visit: https://betterauth.online/docs/

🏗️ Repository Structure

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

📜 License

MIT — see LICENSE.

🔗 Links

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors