lazada-iop-client
Version:
Lazada IOP Client SDK for Node JS
132 lines (90 loc) • 3.66 kB
Markdown
Lazada SDK for JavaScript allows you to interact with the Lazada API easily. This SDK supports features such as authentication, sending API requests, and file handling.
- **OAuth2 Authentication**: Generate an authentication URL for users to authorize your application.
- **API Requests**: Send API requests to the Lazada server with the provided parameters.
- **API Signature**: Sign requests for security and data integrity.
- **File Support**: Upload files to the Lazada API using FormData.
To get started, install the following dependencies:
```bash
pnpm add axios form-data crypto
```
To begin using this SDK, create an instance of `LazadaClient` and provide the required parameters.
```javascript
import LazadaClient from "./LazadaClient";
// Create an instance of LazadaClient
const client = new LazadaClient({
apiKey: "YOUR_API_KEY",
apiSecret: "YOUR_API_SECRET",
region: "SG", // Example: 'SG', 'MY', 'VN', etc.
callbackUrl: "YOUR_REDIRECT_URL",
});
```
To get the authentication URL, use `makeAuthURL`. The user will be directed to this URL to grant access to your app.
```javascript
const authURL = client.makeAuthURL();
console.log(authURL); // Direct the user to this URL to login
```
After the user grants permission and your app receives the authorization code, you can set the access token to the `LazadaClient` instance.
```javascript
client.setAccessToken("YOUR_ACCESS_TOKEN");
```
You can add API parameters that will be used in the API request.
```javascript
client.addAPIParam("key", "value");
client.addFileParam("file_key", fileBuffer);
```
To send an API request to Lazada, use the `execute` method with the API path and HTTP method.
```javascript
const response = await client.execute("/path/api", "GET", { key: "value" });
console.log(response);
```
If you need to change the region where you want to access the Lazada API, you can do so with the `changeRegion` method.
```javascript
client.changeRegion("MY"); // Change to Malaysia
```
```javascript
import LazadaClient from "./LazadaClient";
const client = new LazadaClient({
apiKey: "YOUR_API_KEY",
apiSecret: "YOUR_API_SECRET",
region: "SG",
callbackUrl: "YOUR_REDIRECT_URL",
});
// 1. Get the authentication URL
const authURL = client.makeAuthURL();
console.log("Redirect the user to the following URL:", authURL);
// 2. After getting the access token, set the token
client.setAccessToken("YOUR_ACCESS_TOKEN");
// 3. Add API parameters
client.addAPIParam("someKey", "someValue");
// 4. Execute the API request
client.execute("/some/api/path", "GET").then((response) => {
console.log("Response from the API:", response);
});
```
```
project/
│
├── src/
│ └── index.ts
├── example.ts
├── package.json
└── tsconfig.json
```
If you want to contribute, please open a _pull request_ or create an _issue_ for any problems or suggestions.
This SDK is licensed under the [MIT License](LICENSE).
---
Make sure to replace placeholders like `YOUR_API_KEY`, `YOUR_API_SECRET`, and `YOUR_REDIRECT_URL` with your actual project information.