Basic Usage
Generate Basic Password
Copy
// Generate a basic password
async function generatePassword(length = 16) {
try {
const response = await fetch(`https://api.sinaty.business/password/?length=${length}`);
if (!response.ok) {
const error = await response.json();
throw new Error(error.error);
}
const data = await response.json();
return data;
} catch (error) {
console.error('Error generating password:', error);
throw error;
}
}
// Usage example
generatePassword(20)
.then(data => {
console.log('Generated password:', data.password);
console.log('Strength level:', data.analysis.strength_level);
console.log('Entropy score:', data.analysis.strength_score);
})
.catch(error => {
console.error('Failed to generate password:', error);
});
Custom Password Generation
Copy
// Generate password with custom parameters
async function generateCustomPassword(options = {}) {
const params = new URLSearchParams({
length: options.length || 16,
uppercase: options.uppercase !== false,
lowercase: options.lowercase !== false,
numbers: options.numbers !== false,
symbols: options.symbols !== false,
avoid_similar: options.avoid_similar || false,
avoid_ambiguous: options.avoid_ambiguous || false
});
if (options.custom_chars) {
params.append('custom_chars', options.custom_chars);
}
if (options.exclude_chars) {
params.append('exclude_chars', options.exclude_chars);
}
try {
const response = await fetch(`https://api.sinaty.business/password/?${params}`);
if (!response.ok) {
const error = await response.json();
throw new Error(error.error);
}
return await response.json();
} catch (error) {
console.error('Error generating custom password:', error);
throw error;
}
}
// Usage examples
// Generate user-friendly password
generateCustomPassword({
length: 12,
avoid_similar: true,
avoid_ambiguous: true
}).then(data => console.log('User-friendly password:', data.password));
// Generate API key
generateCustomPassword({
length: 32,
symbols: false,
avoid_similar: true
}).then(data => console.log('API key:', data.password));
Advanced Usage
POST Request Example
Copy
// Generate password using POST method
async function generatePasswordViaPost(config) {
try {
const response = await fetch('https://api.sinaty.business/password/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(config)
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.error);
}
return await response.json();
} catch (error) {
console.error('Error:', error);
throw error;
}
}
// Usage
const config = {
length: 20,
uppercase: true,
lowercase: true,
numbers: true,
symbols: true,
avoid_similar: true
};
generatePasswordViaPost(config)
.then(data => console.log('Generated password:', data.password));
Framework Integration
React Hook Example
Copy
import { useState, useEffect } from 'react';
function usePasswordGenerator(config = {}) {
const [password, setPassword] = useState(null);
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);
const generatePassword = async (options = {}) => {
try {
setLoading(true);
setError(null);
const params = new URLSearchParams({
length: options.length || config.length || 16,
uppercase: options.uppercase !== false,
lowercase: options.lowercase !== false,
numbers: options.numbers !== false,
symbols: options.symbols !== false,
avoid_similar: options.avoid_similar || false,
avoid_ambiguous: options.avoid_ambiguous || false
});
const response = await fetch(`https://api.sinaty.business/password/?${params}`);
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error);
}
const data = await response.json();
setPassword(data);
} catch (err) {
setError(err.message);
} finally {
setLoading(false);
}
};
return { password, loading, error, generatePassword };
}
// Usage in React component
function PasswordGenerator() {
const { password, loading, error, generatePassword } = usePasswordGenerator();
const handleGenerate = () => {
generatePassword({
length: 16,
avoid_similar: true
});
};
return (
<div>
<button onClick={handleGenerate} disabled={loading}>
{loading ? 'Generating...' : 'Generate Password'}
</button>
{error && <div className="error">Error: {error}</div>}
{password && (
<div>
<h3>Generated Password</h3>
<p><strong>Password:</strong> {password.password}</p>
<p><strong>Length:</strong> {password.length}</p>
<p><strong>Strength:</strong> {password.analysis.strength_level}</p>
<p><strong>Entropy:</strong> {password.analysis.strength_score.toFixed(1)} bits</p>
</div>
)}
</div>
);
}
Error Handling
Comprehensive Error Handling
Copy
async function generatePasswordWithErrorHandling(options = {}) {
try {
const params = new URLSearchParams({
length: options.length || 16,
uppercase: options.uppercase !== false,
lowercase: options.lowercase !== false,
numbers: options.numbers !== false,
symbols: options.symbols !== false,
avoid_similar: options.avoid_similar || false,
avoid_ambiguous: options.avoid_ambiguous || false
});
if (options.custom_chars) {
params.append('custom_chars', options.custom_chars);
}
if (options.exclude_chars) {
params.append('exclude_chars', options.exclude_chars);
}
const response = await fetch(`https://api.sinaty.business/password/?${params}`);
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error || `HTTP ${response.status}`);
}
const data = await response.json();
return { success: true, data };
} catch (error) {
return {
success: false,
error: error.message,
timestamp: new Date().toISOString()
};
}
}
// Usage with error handling
generatePasswordWithErrorHandling({ length: 5 }) // Invalid length
.then(result => {
if (result.success) {
console.log('Generated password:', result.data.password);
} else {
console.error('Error:', result.error);
}
});
Support
Need help with integration? We’re here to assist:- Documentation: Check our detailed API documentation
- Examples: Review code examples in our guides
- Email: Contact support@sinaty.business for technical assistance
- Discord: Join our community for developer discussions