React Native
React Native
Amazon SP-API Sellers Get Marketplace Participations
See more Amazon SP-API Examples
Demonstrates Amazon SP-API Sellers API -- get marketplace participations.Chilkat React Native Downloads
import { AuthAws, JsonObject, Rest, StringBuilder } from '@chilkat/react-native'
async function chilkatExample() {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const authAws = new AuthAws();
authAws.accessKey = 'AWS_ACCESS_KEY';
authAws.secretKey = 'AWS_SECRET_KEY';
authAws.serviceName = 'execute-api';
// Use the region that is correct for your needs.
authAws.region = 'eu-west-1';
const rest = new Rest();
const bTls = true;
const port = 443;
const bAutoReconnect = true;
// Make sure to use the correct domain.
// In this example, we are using "sandbox.sellingpartnerapi-eu.amazon.com"
try {
await rest.connectAsync('sandbox.sellingpartnerapi-eu.amazon.com', port, bTls, bAutoReconnect);
} catch {
console.log(rest.lastErrorText);
return;
}
rest.setAuthAws(authAws);
// Load the previously obtained LWA access token.
// See Fetch SP-API LWA Access Token
const jsonToken = new JsonObject();
try {
jsonToken.loadFile('qa_data/tokens/sp_api_lwa_token.json');
} catch {
console.log('Failed to load LWA access token.');
return;
}
// Add the x-amz-access-token request header.
const lwaToken = jsonToken.stringOf('access_token');
rest.clearAllHeaders();
rest.addHeader('x-amz-access-token', lwaToken);
const sbResponse = new StringBuilder();
const uri = '/sellers/v1/marketplaceParticipations';
try {
await rest.fullRequestNoBodySbAsync('GET', uri, sbResponse);
} catch {
console.log(rest.lastErrorText);
return;
}
// Examine the response status.
const statusCode = rest.responseStatusCode;
if (statusCode !== 200) {
console.log(`Response status text: ${rest.responseStatusText}`);
console.log('Response body: ');
console.log(sbResponse.getAsString());
console.log('Failed.');
return;
}
console.log(sbResponse.getAsString());
// If successful, gets a JSON response such as the following:
// {
// "payload": [
// {
// "marketplace": {
// "id": "ATVPDKIKX0DER",
// "countryCode": "US",
// "name": "Amazon.com",
// "defaultCurrencyCode": "USD",
// "defaultLanguageCode": "en_US",
// "domainName": "www.amazon.com"
// },
// "participation": {
// "isParticipating": true,
// "hasSuspendedListings": false
// }
// }
// ]
// }
// Use this online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
const json = new JsonObject();
json.loadSb(sbResponse);
let id = '';
let countryCode = '';
let name = '';
let defaultCurrencyCode = '';
let defaultLanguageCode = '';
let domainName = '';
let i = 0;
const countI = json.sizeOfArray('payload');
while (i < countI) {
json.i = i;
id = json.stringOf('payload[i].marketplace.id');
countryCode = json.stringOf('payload[i].marketplace.countryCode');
name = json.stringOf('payload[i].marketplace.name');
defaultCurrencyCode = json.stringOf('payload[i].marketplace.defaultCurrencyCode');
defaultLanguageCode = json.stringOf('payload[i].marketplace.defaultLanguageCode');
domainName = json.stringOf('payload[i].marketplace.domainName');
json.boolOf('payload[i].participation.isParticipating');
json.boolOf('payload[i].participation.hasSuspendedListings');
i++;
}
console.log('Success!');
}