Authentication overview

Tokopedia's API authentication framework is built to secure interactions between external applications and the Tokopedia marketplace. The primary mechanism for accessing protected resources is OAuth 2.0, an industry-standard protocol for authorization. This approach enables applications to obtain limited access to user accounts on an HTTP service, such as Tokopedia, without requiring users to share their complete credentials with the application itself. Instead, users grant specific permissions, or 'scopes,' to applications, which then receive an access token to perform actions on their behalf.

The developer experience for Tokopedia API authentication emphasizes a secure and controlled environment for partners and sellers. By leveraging OAuth 2.0, Tokopedia aims to protect user data while facilitating programmatic access for operations like product listing, order management, and inventory synchronization. Applications must register with Tokopedia's developer portal to obtain the necessary client credentials before they can initiate the OAuth flow and request access tokens.

Understanding the OAuth 2.0 grant types available is crucial for integrating with Tokopedia. The choice of grant type depends on the nature of the application (e.g., web application, mobile app, server-side integration) and how it interacts with user data. Each grant type specifies a different flow for obtaining an access token, ensuring flexibility and security for various integration scenarios. The Tokopedia developer documentation provides detailed guides on implementing these flows for specific use cases, ensuring that developers can integrate securely and efficiently with the platform's extensive e-commerce capabilities.

Supported authentication methods

Tokopedia primarily supports OAuth 2.0 for API authentication, which involves several steps to obtain and use access tokens. This method is considered a robust and widely adopted standard for securing API access. The core components of the OAuth 2.0 flow include the Client ID, Client Secret, Authorization Code, Access Token, and Refresh Token.

OAuth 2.0 Grant Types

Tokopedia's API supports different OAuth 2.0 grant types to accommodate various application architectures. While the specific grant types supported are detailed in the official Tokopedia API reference documentation, common grant types include:

  • Authorization Code Grant: This is the most common and recommended grant type for web applications. It involves redirecting the user to Tokopedia to authorize the application, after which an authorization code is exchanged for an access token and refresh token. This method provides the highest level of security by keeping the client secret confidential on the server side.
  • Client Credentials Grant: Suitable for server-to-server interactions where the application needs to access its own service account rather than a specific user's account. This grant type directly exchanges client credentials (Client ID and Client Secret) for an access token without user involvement.

Token Management

Once an access token is obtained, it must be included in the Authorization header of subsequent API requests. Access tokens are typically short-lived for security reasons. When an access token expires, the application can use a refresh token (if obtained) to request a new access token without requiring the user to re-authorize. Refresh tokens are generally long-lived and should be stored securely.

Tokopedia API Authentication Methods
Method When to Use Security Level
OAuth 2.0 (Authorization Code Grant) Web applications, user-facing integrations requiring user consent High (client secret kept server-side, user consent required)
OAuth 2.0 (Client Credentials Grant) Server-to-server communication, internal services accessing own data Moderate-High (depends on client secret security)
Access Token (Bearer Token) All authenticated API requests after token issuance Depends on token handling (should be secured via HTTPS, short-lived)

Getting your credentials

To begin integrating with the Tokopedia API and obtain the necessary authentication credentials, you must register your application on the Tokopedia Developer Portal. This process typically involves creating a developer account and then registering a new application, which will generate your unique Client ID and Client Secret.

Steps to obtain credentials:

  1. Register as a Developer: Navigate to the Tokopedia Developer Portal and sign up for a developer account. This usually requires a Tokopedia user account.
  2. Create a New Application: Once logged in, locate the section for 'My Apps' or 'Applications' and choose to create a new application. You will need to provide details such as your application's name, description, and importantly, the Redirect URI(s).
  3. Configure Redirect URIs: The Redirect URI (also known as Callback URL) is a critical security component in the OAuth 2.0 flow. It specifies where Tokopedia will redirect the user's browser after they have authorized your application. This URI must be pre-registered with Tokopedia and must be an HTTPS URL for production environments. Ensure this URI is accurate and secure, as it prevents authorization codes from being intercepted by malicious parties.
  4. Retrieve Client ID and Client Secret: Upon successful application registration, Tokopedia will issue a unique Client ID and Client Secret for your application. The Client ID is publicly exposed and used to identify your application. The Client Secret is a confidential key that must be kept secure and never exposed in client-side code.
  5. Set up Scopes: Define the specific permissions, or 'scopes,' your application requires. These scopes dictate which types of user data and API functionalities your application can access (e.g., read_products, write_orders). Requesting only the necessary scopes adheres to the principle of least privilege and enhances security.

Once you have your Client ID, Client Secret, and configured Redirect URIs, you can initiate the OAuth 2.0 authorization flow to obtain access tokens. It is highly recommended to review the Tokopedia API documentation for the most current and detailed instructions on credential setup and OAuth flow implementation.

Authenticated request example

After successfully completing the OAuth 2.0 flow and obtaining an access token, you can use it to make authenticated requests to the Tokopedia API. The access token is typically sent in the Authorization header of your HTTP request, using the Bearer scheme.

Example: Retrieving User Profile (Conceptual)

Let's assume you have obtained an access token (YOUR_ACCESS_TOKEN) and want to retrieve a user's profile information. This example demonstrates a conceptual request using curl, a common command-line tool for making HTTP requests.


curl -X GET \
  'https://developer.tokopedia.com/v1/user/profile' \
  -H 'Authorization: Bearer YOUR_ACCESS_TOKEN' \
  -H 'Content-Type: application/json'

In this example:

  • GET specifies the HTTP method, indicating that you are requesting data.
  • https://developer.tokopedia.com/v1/user/profile is the conceptual API endpoint for retrieving user profile information. Actual endpoints are detailed in the Tokopedia API reference.
  • Authorization: Bearer YOUR_ACCESS_TOKEN is the crucial header containing your access token. Replace YOUR_ACCESS_TOKEN with the actual token you received.
  • Content-Type: application/json indicates that the request body (if any) and expected response format are JSON.

Upon a successful request, the Tokopedia API will return the requested user profile data in JSON format, provided your access token has the necessary scopes and is still valid. If the token is invalid or expired, the API will typically return an HTTP 401 Unauthorized or 403 Forbidden status code.

Security best practices

Adhering to security best practices is essential when integrating with the Tokopedia API to protect user data and maintain the integrity of your application. The following recommendations are based on general API security principles and specific considerations for OAuth 2.0 implementations.

1. Secure your Client Secret

  • Server-Side Storage: Never embed your Client Secret directly in client-side code (e.g., JavaScript, mobile app binaries). It must always be stored and used on a secure server.
  • Environment Variables/Key Vaults: Store Client Secrets in environment variables or dedicated secret management services (e.g., AWS Secrets Manager, Google Secret Manager, Azure Key Vault) rather than hardcoding them in your application's source code.
  • Access Control: Restrict access to your Client Secret to only necessary personnel and systems.

2. Protect Access and Refresh Tokens

  • HTTPS Everywhere: Always use HTTPS for all communication with the Tokopedia API and for all redirects during the OAuth flow. This encrypts data in transit, preventing eavesdropping.
  • Secure Storage: Store refresh tokens securely on your server, ideally encrypted at rest. Avoid storing them in client-side storage mechanisms like local storage or cookies without proper security measures (e.g., HttpOnly, SameSite attributes).
  • Short-Lived Access Tokens: Tokopedia's access tokens are typically short-lived. Implement a mechanism to refresh tokens before they expire using the refresh token, minimizing the window of opportunity for token misuse.
  • Token Revocation: Implement token revocation mechanisms if Tokopedia provides them. This allows you to invalidate compromised tokens immediately.

3. Validate Redirect URIs

  • Strict Matching: Ensure that the Redirect URIs registered in your Tokopedia developer application exactly match the URIs used in your OAuth authorization requests. Any mismatch should be rejected by the authorization server.
  • HTTPS Only: Only register and use HTTPS Redirect URIs in production environments.

4. Implement State Parameter

  • CSRF Protection: Use the state parameter in your OAuth authorization requests to prevent Cross-Site Request Forgery (CSRF) attacks. The state parameter should be a unique, unguessable value generated by your application and verified upon receiving the callback from Tokopedia.

5. Scope Management

  • Least Privilege: Request only the minimum necessary scopes required for your application's functionality. Avoid requesting broad permissions if not strictly needed. This limits the potential impact if your application is compromised.

6. Error Handling and Logging

  • Secure Logging: Log authentication failures and suspicious activities, but never log sensitive information like access tokens, refresh tokens, or client secrets.
  • Graceful Error Handling: Implement robust error handling for API responses, especially for authentication and authorization errors, to provide clear feedback without exposing sensitive system details.

7. Regular Security Audits

  • Code Review: Regularly review your code for security vulnerabilities, especially concerning credential handling and API interactions.
  • Dependency Updates: Keep all libraries and frameworks used in your application up to date to patch known security vulnerabilities.

By following these best practices, developers can create more secure integrations with the Tokopedia API, protecting both their applications and the data of Tokopedia users. For further details on secure OAuth 2.0 implementations, refer to the OAuth 2.0 Authorization Framework specification and Tokopedia's official security guidelines in their developer documentation.