V1 Authentication
Login the user to get the access_token
To interact with the Chrono Shift API, you need to authenticate using the provided /api/v1/authenticate
endpoint. This endpoint allows you to log in using your email and password, and it returns a JWT token that you will use for authenticated requests to other endpoints.
Endpoint: /api/v1/authenticate
Method: POST
Description: Authenticates a user and returns a JWT token if the login is successful.
Request
URL: /api/v1/authenticate
Method: POST
Headers:
Content-Type
:application/json
Body:
code{
"email": "[email protected]",
"password": "your_password"
}
Response
Success (HTTP Status: 200):
code{
"status": 200,
"access_token": "jwt_token_here"
}
Failure (HTTP Status: 400):
code{
"status": 400,
"msg": "Email and password required"
}
Failure (HTTP Status: 401):
code{
"status": 401,
"msg": "Bad email or password"
}
Usage Instructions
Send a Login Request:
Make a POST request to
/api/v1/authenticate
with the user's email and password in the request body.
Receive the JWT Token:
Upon successful authentication, the response will include a JWT token in the
access_token
field. Store this token securely; you will need it for all subsequent authenticated API requests.
Include the JWT Token in Subsequent Requests:
For endpoints that require authentication, include the JWT token in the
Authorization
header of your requests:Authorization: Bearer jwt_token_here
Example Code in JavaScript
Below is an example of how you might use the /api/v1/authenticate
endpoint in JavaScript using the fetch
API:
const login = async (email, password) => {
const url = 'https://dwarfs.store/api/v1/authenticate';
const payload = {
email: email,
password: password
};
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
});
if (response.ok) {
const data = await response.json();
const accessToken = data.access_token;
console.log(`Access Token: ${accessToken}`);
return accessToken;
} else {
const errorData = await response.json();
console.error(`Failed to authenticate: ${errorData.msg}`);
}
};
// Usage
login('[email protected]', 'your_password');
Fetch Dwarfs by User ID
The Chrono Dwarfs API also provides an endpoint to fetch dwarfs data by user ID. This endpoint requires authentication via the JWT token obtained from the /api/v1/authenticate
endpoint.
Endpoint: /api/v1/users/<user_id>/dwarfs
Method: GET
Description: Fetches all dwarfs data associated with the specified user ID.
Request
URL: /api/v1/users/<user_id>/dwarfs
Method: GET
Headers:
Authorization
:Bearer jwt_token_here
Content-Type
:application/json
Response
Success (HTTP Status: 200):
{
"status": 200,
"dwarfs": [
{
"id": 1,
"name": "Elys",
"description": "Fearless warrior that had seen a lot of cruelty in the world",
"speciality": "Warrior",
"rarity": "Rare",
"times_cloned": 2,
"generation": 1,
"is_grown": true,
"minted_date": "2024-06-31",
"view_count": 783,
"race": "Alive",
"background": "Forest",
"hat": "Iron Helmet",
"face": "Grim",
"body": "Armored",
"pants": "Leather",
"weapon": "Axe",
"story": "A fierce warrior from the mountains.",
"skill_1_name": "Slash",
"skill_1_type": "Attack",
"skill_2_name": "Shield Block",
"skill_2_type": "Defense",
"skill_3_name": "Battle Cry",
"skill_3_type": "Buff",
"mint_address": "0x1234abcd...",
"is_sellable": true,
"sell_core_currency": "DWRF",
"sell_amount": 100
},
// Other dwarf objects...
]
}
Failure (HTTP Status: 401):
code{
"status": 401,
"msg": "Unauthorized access"
}
Usage Instructions
Authenticate:
Use the
/api/v1/authenticate
endpoint to obtain a JWT token.
Send a Request to Fetch Dwarfs Data:
Make a GET request to
/api/v1/users/<user_id>/dwarfs
with the JWT token included in theAuthorization
header.
Example Code in JavaScript
Below is an example of how you might use the /api/v1/users/<user_id>/dwarfs
endpoint in JavaScript using the fetch
API:
const fetchDwarfs = async (userId, accessToken) => {
const url = `https://yourdomain.com/api/v1/users/${userId}/dwarfs`;
const response = await fetch(url, {
method: 'GET',
headers: {
'Authorization': `Bearer ${accessToken}`,
'Content-Type': 'application/json'
}
});
if (response.ok) {
const data = await response.json();
console.log('Dwarfs Data:', data.dwarfs);
return data.dwarfs;
} else {
const errorData = await response.json();
console.error(`Failed to fetch dwarfs data: ${errorData.msg}`);
}
};
// Usage
const userId = 1; // Replace with the actual user ID
const accessToken = 'your_jwt_token_here'; // Replace with the actual JWT token obtained from login
fetchDwarfs(userId, accessToken);
Last updated
Was this helpful?