The Bookshop API uses token-based authentication to secure the endpoints and protect user data. All requests to protected endpoints must include a valid authentication token in the request header.
To interact with the Bookshop API, you first need to authenticate by obtaining a token. Typically, you would send a request to a login endpoint with your credentials (e.g., username and password). The server will respond with an authentication token if the credentials are valid.
POST /api/auth/login
Name | Type | Required | Description |
---|---|---|---|
username | string | Yes | The username for authentication. |
password | string | Yes | The password for authentication. |
fetch("/api/auth/login", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
username: "user123",
password: "securePassword",
}),
})
.then((response) => response.json())
.then((data) => {
// Save the authentication token for future requests
const token = data.token;
console.log("Authentication Token:", token);
})
.catch((error) => console.error("Error:", error));
{
"token": "your-authentication-token-here"
}
Once you have obtained the authentication token, you must include it in the Authorization header of all subsequent API requests. The token should be prefixed with the word "Bearer".
To make a request to a protected endpoint, such as fetching all books, include the token in the Authorization header:
fetch('/api/books', {
method: 'GET',
headers: {
'Authorization': 'Bearer your-authentication-token-here'
}
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
If the token is missing, expired, or invalid, the server will respond with a 401 Unauthorized
status code. It's important to handle these errors gracefully in your application.
fetch('/api/books', {
method: 'GET',
headers: {
'Authorization': 'Bearer invalid-or-expired-token'
}
})
.then(response => {
if (response.status === 401) {
throw new Error('Unauthorized: Please check your authentication token.');
}
return response.json();
})
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
For long-lived sessions, you may need to refresh the token periodically. The API may provide a token refresh endpoint to extend the token's validity without requiring the user to log in again.
POST /api/auth/refresh
Header | Required | Description |
---|---|---|
Authorization | Yes | The current token in "Bearer" format. |
fetch('/api/auth/refresh', {
method: 'POST',
headers: {
'Authorization': 'Bearer your-current-authentication-token'
}
})
.then(response => response.json())
.then(data => {
// Save the new authentication token
const newToken = data.token;
console.log('New Authentication Token:', newToken);
})
.catch(error => console.error('Error:', error));
{
"token": "new-authentication-token-here"
}
With these guidelines, you can securely interact with the Bookshop API while protecting user data and ensuring a smooth user experience.