API Documentation
Integrate random number generation into your applications
Introduction
The RandomNumber.net API provides programmatic access to our random number generation services. With this API, you can integrate high-quality random number generation into your own applications, websites, or services.
Our API is RESTful and uses JSON for request and response bodies. All API endpoints are accessed via HTTPS to ensure data security during transmission.
Base URL: https://randomnumber.net/api/
Authentication
The RandomNumber.net API uses CSRF tokens for authentication. To make API requests, you need to:
- Include a valid CSRF token in the
X-CSRF-Tokenheader of your request - Send requests from a browser that has a valid session with RandomNumber.net
CSRF tokens are automatically generated when you visit the RandomNumber.net website and are
available in the global JavaScript variable CSRF_TOKEN.
Example of including the CSRF token in a fetch request:
fetch('https://randomnumber.net/api/generate.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRF-Token': CSRF_TOKEN
},
body: JSON.stringify({
min: 1,
max: 100,
count: 5
})
})
Rate Limits
To ensure fair usage and system stability, the API has the following rate limits:
- 100 requests per hour per IP address
- Maximum of 10,000 random numbers per request
If you exceed these limits, the API will return a 429 Too Many Requests response.
If you need higher limits for your application, please contact us.
Error Handling
The API uses standard HTTP status codes to indicate the success or failure of a request:
200 OK- The request was successful400 Bad Request- The request was invalid or missing required parameters403 Forbidden- Authentication failed or you don't have permission to access the resource429 Too Many Requests- You've exceeded the rate limit500 Internal Server Error- An error occurred on the server
Error responses include a JSON object with an error field that provides more information about the error:
Example error response:
{
"error": "Count cannot exceed the range of possible values when duplicates are not allowed"
}
Endpoints
Generate Random Numbers
Generate one or more random numbers within a specified range.
URL: /api/generate.php
Method: POST
Content Type: application/json
Request Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
min |
Integer | Yes | The minimum value (inclusive). Must be between -1,000,000,000 and 1,000,000,000. |
max |
Integer | Yes | The maximum value (inclusive). Must be between -1,000,000,000 and 1,000,000,000. |
count |
Integer | Yes | The number of random numbers to generate. Must be between 1 and 10,000. |
allowDuplicates |
Boolean | No | Whether to allow duplicate numbers in the result. Default is true. |
Response
On success, the API returns a JSON object with the following fields:
| Field | Type | Description |
|---|---|---|
numbers |
Array of Integers | The generated random numbers. |
timestamp |
Integer | The Unix timestamp when the numbers were generated. |
hash |
String | A verification hash that can be used to verify the authenticity of the numbers. |
Example request:
POST /api/generate.php
Content-Type: application/json
X-CSRF-Token: your_csrf_token
{
"min": 1,
"max": 100,
"count": 5,
"allowDuplicates": false
}
Example response:
{
"numbers": [23, 47, 12, 89, 56],
"timestamp": 1740693045,
"hash": "8f7d6a5e4c3b2a1098765432109876543210abcdef1234567890abcdef123456"
}
Generate Password
Generate a random password with customizable parameters.
URL: /api/generate-password.php
Method: POST
Content Type: application/json
Request Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
length |
Integer | Yes | The length of the password. Must be between 4 and 100. |
includeUppercase |
Boolean | No | Whether to include uppercase letters. Default is true. |
includeLowercase |
Boolean | No | Whether to include lowercase letters. Default is true. |
includeNumbers |
Boolean | No | Whether to include numbers. Default is true. |
includeSymbols |
Boolean | No | Whether to include symbols. Default is true. |
Response
On success, the API returns a JSON object with the following fields:
| Field | Type | Description |
|---|---|---|
password |
String | The generated password. |
Example request:
POST /api/generate-password.php
Content-Type: application/json
X-CSRF-Token: your_csrf_token
{
"length": 12,
"includeUppercase": true,
"includeLowercase": true,
"includeNumbers": true,
"includeSymbols": false
}
Example response:
{
"password": "A7bX2pQ9cR5d"
}
Verify Random Numbers
Verify the authenticity of random numbers generated by the API.
URL: /api/verify.php
Method: POST
Content Type: application/json
Request Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
numbers |
Array of Integers | Yes | The numbers to verify. |
timestamp |
Integer | Yes | The Unix timestamp when the numbers were generated. |
hash |
String | Yes | The verification hash to check against. |
Response
On success, the API returns a JSON object with the following fields:
| Field | Type | Description |
|---|---|---|
verified |
Boolean | Whether the numbers were successfully verified. |
calculatedHash |
String | The hash calculated from the provided numbers and timestamp. |
Example request:
POST /api/verify.php
Content-Type: application/json
X-CSRF-Token: your_csrf_token
{
"numbers": [23, 47, 12, 89, 56],
"timestamp": 1740693045,
"hash": "8f7d6a5e4c3b2a1098765432109876543210abcdef1234567890abcdef123456"
}
Example response:
{
"verified": true,
"calculatedHash": "8f7d6a5e4c3b2a1098765432109876543210abcdef1234567890abcdef123456"
}
Code Examples
// Generate random numbers
async function generateRandomNumbers(min, max, count, allowDuplicates = true) {
try {
const response = await fetch('https://randomnumber.net/api/generate.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRF-Token': CSRF_TOKEN
},
body: JSON.stringify({
min: min,
max: max,
count: count,
allowDuplicates: allowDuplicates
})
});
if (!response.ok) {
throw new Error('Network response was not ok');
}
return await response.json();
} catch (error) {
console.error('Error generating random numbers:', error);
throw error;
}
}
// Example usage
generateRandomNumbers(1, 100, 5, false)
.then(result => {
console.log('Random numbers:', result.numbers);
console.log('Timestamp:', result.timestamp);
console.log('Verification hash:', result.hash);
})
.catch(error => {
console.error('Error:', error);
});
<?php
// Generate random numbers
function generateRandomNumbers($min, $max, $count, $allowDuplicates = true) {
$url = 'https://randomnumber.net/api/generate.php';
$data = [
'min' => $min,
'max' => $max,
'count' => $count,
'allowDuplicates' => $allowDuplicates
];
// Get CSRF token from session
session_start();
$csrf_token = $_SESSION['csrf_token'];
$options = [
'http' => [
'header' => "Content-type: application/json\r\nX-CSRF-Token: $csrf_token\r\n",
'method' => 'POST',
'content' => json_encode($data)
]
];
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) {
throw new Exception('Error generating random numbers');
}
return json_decode($result, true);
}
// Example usage
try {
$result = generateRandomNumbers(1, 100, 5, false);
echo "Random numbers: " . implode(', ', $result['numbers']) . "\n";
echo "Timestamp: " . $result['timestamp'] . "\n";
echo "Verification hash: " . $result['hash'] . "\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
?>
import requests
import json
# Generate random numbers
def generate_random_numbers(min_val, max_val, count, allow_duplicates=True):
url = 'https://randomnumber.net/api/generate.php'
data = {
'min': min_val,
'max': max_val,
'count': count,
'allowDuplicates': allow_duplicates
}
# You need to get the CSRF token from a browser session
csrf_token = 'your_csrf_token'
headers = {
'Content-Type': 'application/json',
'X-CSRF-Token': csrf_token
}
response = requests.post(url, headers=headers, data=json.dumps(data))
if response.status_code != 200:
raise Exception(f'Error generating random numbers: {response.text}')
return response.json()
# Example usage
try:
result = generate_random_numbers(1, 100, 5, False)
print(f"Random numbers: {result['numbers']}")
print(f"Timestamp: {result['timestamp']}")
print(f"Verification hash: {result['hash']}")
except Exception as e:
print(f"Error: {e}")
Support
If you have any questions or need help with the API, please contact us.
For bug reports or feature requests, please email support@randomnumber.net.