How to Fix 403 Forbidden Error in Shiprocket API Integration?

When integrating the Shiprocket order creation into your Spring Boot application, encountering a 403 Forbidden error can be quite frustrating. This issue typically indicates that the server understood your request, but it refuses to authorize it. Here, we’ll explore why this might happen and provide a working example to help you successfully create an order using Shiprocket's API. Understanding the 403 Forbidden Error The 403 Forbidden error during an API request often arises when a user is not authorized to access the endpoint. In the case of the Shiprocket API, there are several reasons you might experience this issue: Invalid Token: Even though your token appears valid, there could be issues with its generation or scope. Insufficient Permissions: The account associated with your API key may not have permission to create orders. Endpoint Errors: The API endpoint URL could be incorrect or deprecated. Request Limits: You may have hit a rate limit imposed by Shiprocket. By closely analyzing these aspects, you can better diagnose the root cause of the error. Now, let's ensure your order creation payload and API calls are correctly configured. Step-by-Step Solution 1. Create an Authentication Token Start with authenticating against Shiprocket's login API to retrieve your token. Here’s an example of proper Java code using Spring’s WebClient: public class ShiprocketAuthResponse { private String token; // Getters and Setters } public String getToken() { WebClient webClient = WebClient.create(); return webClient.post() .uri("https://apiv2.shiprocket.in/v1/external/auth/login") .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) .bodyValue(Map.of("email", "your-email", "password", "your-password")) .retrieve() .bodyToMono(ShiprocketAuthResponse.class) .block() .getToken(); } 2. Check the Order Creation Request Once you have your authenticated token, you can proceed to create an order. Below is a working example of the order creation request that you could implement: public String createOrder(String token, Map orderRequestPayload) { WebClient webClient = WebClient.create(); return webClient.post() .uri("https://apiv2.shiprocket.in/v1/external/orders/create/adhoc") .header("Authorization", "Bearer " + token) .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) .bodyValue(orderRequestPayload) .retrieve() .bodyToMono(String.class) .block(); } 3. Sample Order Payload Your order payload must adhere to Shiprocket's API specifications. Here’s an example of a payload you might need: { "order_id": "ORD123456", "order_date": "2023-10-01T10:00:00", "pickup_location": "pickup_location", "billing_country": "India", "billing_state": "Delhi", "billing_city": "New Delhi", "billing_street": "123 Main St", "billing_pincode": "110001", "billing_email": "customer@example.com", "billing_phone": "9876543210", "items": [ { "name": "Product 1", "sku": "SKU123", "units": 1, "selling_price": 500, "discount": 0, "tax": 50 } ] } Make sure your payload matches the specifications provided in the Shiprocket API documentation. Common Pitfalls to Avoid Use the Updated API URL: Always reference the latest API documentation to ensure you're using the current endpoint. Verify Content-Type: Incorrect content types can lead to API failures; ensure you're sending application/json. Permissions Check: Ensure your user account has sufficient privileges. Error Logging: Implement logging to capture the full error response, which can provide more detail beyond a simple 403 status. Frequently Asked Questions What should I do if my token is valid but still getting a 403 error? Ensure that you have sufficient permissions on your Shiprocket account and that the endpoint accesses you are attempting to reach are appropriately scoped. How can I verify my API key permissions? Reach out to Shiprocket support or check through your account settings to confirm your API key's permissions and roles. Should I retry after a 403 error? Continuously retrying may not solve the issue. Instead, investigate the underlying causes, handle authentication correctly, and check for permission settings. Conclusion Integrating the Shiprocket order creation API in your Spring Boot application can be straightforward once you have the right configurations and approach. By ensuring your token is valid, your request payload adheres to expected formats, and you’re working within the permissions granted by your API key, you should be able to overcome the 403 Forbidden error. If problems persist, double-check the API documentation and consider reaching out to their support team for assistance.

May 13, 2025 - 09:14
 0
How to Fix 403 Forbidden Error in Shiprocket API Integration?

When integrating the Shiprocket order creation into your Spring Boot application, encountering a 403 Forbidden error can be quite frustrating. This issue typically indicates that the server understood your request, but it refuses to authorize it. Here, we’ll explore why this might happen and provide a working example to help you successfully create an order using Shiprocket's API.

Understanding the 403 Forbidden Error

The 403 Forbidden error during an API request often arises when a user is not authorized to access the endpoint. In the case of the Shiprocket API, there are several reasons you might experience this issue:

  1. Invalid Token: Even though your token appears valid, there could be issues with its generation or scope.
  2. Insufficient Permissions: The account associated with your API key may not have permission to create orders.
  3. Endpoint Errors: The API endpoint URL could be incorrect or deprecated.
  4. Request Limits: You may have hit a rate limit imposed by Shiprocket.

By closely analyzing these aspects, you can better diagnose the root cause of the error. Now, let's ensure your order creation payload and API calls are correctly configured.

Step-by-Step Solution

1. Create an Authentication Token

Start with authenticating against Shiprocket's login API to retrieve your token. Here’s an example of proper Java code using Spring’s WebClient:

public class ShiprocketAuthResponse {
    private String token;
    // Getters and Setters
}

public String getToken() {
    WebClient webClient = WebClient.create();
    return webClient.post()
        .uri("https://apiv2.shiprocket.in/v1/external/auth/login")
        .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
        .bodyValue(Map.of("email", "your-email", "password", "your-password"))
        .retrieve()
        .bodyToMono(ShiprocketAuthResponse.class)
        .block()
        .getToken();
}

2. Check the Order Creation Request

Once you have your authenticated token, you can proceed to create an order. Below is a working example of the order creation request that you could implement:

public String createOrder(String token, Map orderRequestPayload) {
    WebClient webClient = WebClient.create();
    return webClient.post()
        .uri("https://apiv2.shiprocket.in/v1/external/orders/create/adhoc")
        .header("Authorization", "Bearer " + token)
        .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
        .bodyValue(orderRequestPayload)
        .retrieve()
        .bodyToMono(String.class)
        .block();
}

3. Sample Order Payload

Your order payload must adhere to Shiprocket's API specifications. Here’s an example of a payload you might need:

{
    "order_id": "ORD123456",
    "order_date": "2023-10-01T10:00:00",
    "pickup_location": "pickup_location",
    "billing_country": "India",
    "billing_state": "Delhi",
    "billing_city": "New Delhi",
    "billing_street": "123 Main St",
    "billing_pincode": "110001",
    "billing_email": "customer@example.com",
    "billing_phone": "9876543210",
    "items": [
        {
            "name": "Product 1",
            "sku": "SKU123",
            "units": 1,
            "selling_price": 500,
            "discount": 0,
            "tax": 50
        }
    ]
}

Make sure your payload matches the specifications provided in the Shiprocket API documentation.

Common Pitfalls to Avoid

  • Use the Updated API URL: Always reference the latest API documentation to ensure you're using the current endpoint.
  • Verify Content-Type: Incorrect content types can lead to API failures; ensure you're sending application/json.
  • Permissions Check: Ensure your user account has sufficient privileges.
  • Error Logging: Implement logging to capture the full error response, which can provide more detail beyond a simple 403 status.

Frequently Asked Questions

What should I do if my token is valid but still getting a 403 error?

Ensure that you have sufficient permissions on your Shiprocket account and that the endpoint accesses you are attempting to reach are appropriately scoped.

How can I verify my API key permissions?

Reach out to Shiprocket support or check through your account settings to confirm your API key's permissions and roles.

Should I retry after a 403 error?

Continuously retrying may not solve the issue. Instead, investigate the underlying causes, handle authentication correctly, and check for permission settings.

Conclusion

Integrating the Shiprocket order creation API in your Spring Boot application can be straightforward once you have the right configurations and approach. By ensuring your token is valid, your request payload adheres to expected formats, and you’re working within the permissions granted by your API key, you should be able to overcome the 403 Forbidden error. If problems persist, double-check the API documentation and consider reaching out to their support team for assistance.