Unauthorized Error

Overview

An Unauthorized error occurs when a request lacks valid authentication credentials or the provided credentials are invalid. This error is returned with HTTP status code 401.

This error indicates that the request cannot be processed because it lacks proper authorization. You need to authenticate with valid credentials before accessing the requested resource.

Error Details

  • Type: https://bessapay.net/errors/unauthorized
  • Status: 401 Unauthorized
  • Error Code: UNAUTHORIZED
  • Title: Authentication credentials are missing or invalid

Common Scenarios

  • Missing API key in the request header
  • Invalid or expired API key
  • Invalid or expired authentication token
  • API key with insufficient permissions for the requested resource
  • Authentication credentials in incorrect format

Example Error Response

Unauthorized Error Response
1{
2  "type": "https://bessapay.net/errors/unauthorized",
3  "title": "Authentication credentials are missing or invalid",
4  "status": 401,
5  "detail": "The request lacks valid authentication credentials for the requested resource",
6  "instance": "/api/v1/transactions/123",
7  "error_code": "UNAUTHORIZED",
8  "timestamp": "2023-06-15T08:12:34Z"
9}

How to Fix

1. Check your API key

Ensure you are including a valid API key in the Authorization header of your request.

Authorization Header
1# Example of a proper Authorization header
2Authorization: Bearer your-api-key-here

2. Verify API key is active

Check that your API key is not expired or revoked. You can verify this in your BessaPay dashboard.

3. Ensure proper permissions

Make sure your API key has the necessary permissions to access the requested resource. Some resources may require specific scopes or roles.

4. Regenerate your API key

If your API key is compromised or no longer working, generate a new one from your BessaPay dashboard. Remember to update all your implementations to use the new key.

Code Example: Handling Unauthorized Errors

JavaScript Error Handling
1// Example of handling unauthorized errors in JavaScript
2async function fetchData() {
3  try {
4    const response = await fetch('https://api.semuni.com/v1/transactions', {
5      headers: {
6        'Authorization': `Bearer ${apiKey}`,
7        'Content-Type': 'application/json'
8      }
9    });
10    
11    if (response.status === 401) {
12      const errorData = await response.json();
13      console.error('Authentication error:', errorData);
14      
15      // Handle the unauthorized error
16      if (errorData.error_code === 'UNAUTHORIZED') {
17        // Redirect to login or refresh authentication
18        redirectToLogin();
19        // Or refresh the token
20        // await refreshAuthToken();
21      }
22      return;
23    }
24    
25    const data = await response.json();
26    return data;
27  } catch (error) {
28    console.error('Error fetching data:', error);
29  }
30}