React Native
React Native
Lightspeed - Create a Product
See more Lightspeed Examples
Create a new product based on the given parameters.Chilkat React Native Downloads
import { Http, HttpResponse, JsonObject, StringBuilder } from '@chilkat/react-native'
async function chilkatExample() {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const http = new Http();
// Implements the following CURL command:
// curl -u API_KEY:API_SECRET \
// -H "Content-Type: application/json" \
// -X POST \
// -d '{
// "product": {
// "visibility": "visible",
// "data01": "",
// "data02": "",
// "data03": "",
// "title": "Lookin Sharp T-Shirt",
// "fulltitle": "Lookin Sharp T-Shirt",
// "description": "Description of the Lookin Sharp T-Shirt",
// "content": "Long Description of the Lookin Sharp T-Shirt",
// "deliverydate": 6488,
// "supplier": 78794,
// "brand": 1171202
// }
// }' \
// "https://api.webshopapp.com/en/products.json"
// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code
http.login = 'API_KEY';
http.password = 'API_SECRET';
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "product": {
// "visibility": "visible",
// "data01": "",
// "data02": "",
// "data03": "",
// "title": "Lookin Sharp T-Shirt",
// "fulltitle": "Lookin Sharp T-Shirt",
// "description": "Description of the Lookin Sharp T-Shirt",
// "content": "Long Description of the Lookin Sharp T-Shirt",
// "deliverydate": 6488,
// "supplier": 78794,
// "brand": 1171202
// }
// }
const json = new JsonObject();
json.updateString('product.visibility', 'visible');
json.updateString('product.data01', '');
json.updateString('product.data02', '');
json.updateString('product.data03', '');
json.updateString('product.title', 'Lookin Sharp T-Shirt');
json.updateString('product.fulltitle', 'Lookin Sharp T-Shirt');
json.updateString('product.description', 'Description of the Lookin Sharp T-Shirt');
json.updateString('product.content', 'Long Description of the Lookin Sharp T-Shirt');
json.updateInt('product.deliverydate', 6488);
json.updateInt('product.supplier', 78794);
json.updateInt('product.brand', 1171202);
http.setRequestHeader('Content-Type', 'application/json');
// Use the correct cluster for your shop. Here are the choices:
// eu1 https://api.webshopapp.com/en/
// us1 https://api.shoplightspeed.com/en/
const resp = new HttpResponse();
try {
await http.httpJsonAsync('POST', 'https://api.webshopapp.com/en/products.json', json, 'application/json', resp);
} catch {
console.log(http.lastErrorText);
return;
}
const sbResponseBody = new StringBuilder();
resp.getBodySb(sbResponseBody);
const jResp = new JsonObject();
jResp.loadSb(sbResponseBody);
jResp.emitCompact = false;
console.log('Response Body:');
console.log(jResp.emit());
const respStatusCode = resp.statusCode;
console.log(`Response Status Code = ${respStatusCode}`);
if (respStatusCode >= 400) {
console.log('Response Header:');
console.log(resp.header);
console.log('Failed.');
return;
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "product": {
// "id": 20974460,
// "createdAt": "2019-05-28T20:14:26+00:00",
// "updatedAt": "2019-05-28T20:14:26+00:00",
// "isVisible": true,
// "visibility": "visible",
// "hasMatrix": false,
// "data01": "",
// "data02": "",
// "data03": "",
// "url": "lookin-sharp-t-shirt",
// "title": "Lookin' Sharp T-Shirt",
// "fulltitle": "Lookin' Sharp T-Shirt",
// "description": "Description of the Lookin' Sharp T-Shirt",
// "content": "<p>Long Description of the Lookin' Sharp T-Shirt</p>",
// "set": false,
// "brand": {
// "resource": {
// "id": 1171202,
// "url": "brands/1171202",
// "link": "https://api.shoplightspeed.com/us/brands/1171202.json"
// }
// },
// "categories": {
// "resource": {
// "id": false,
// "url": "categories/products?product=20974460",
// "link": "https://api.shoplightspeed.com/us/categories/products.json?product=20974460"
// }
// },
// "deliverydate": {
// "resource": {
// "id": 6488,
// "url": "deliverydates/6488",
// "link": "https://api.shoplightspeed.com/us/deliverydates/6488.json"
// }
// },
// "image": false,
// "images": false,
// "relations": {
// "resource": {
// "id": false,
// "url": "products/20974460/relations",
// "link": "https://api.shoplightspeed.com/us/products/20974460/relations.json"
// }
// },
// "metafields": {
// "resource": {
// "id": false,
// "url": "products/20974460/metafields",
// "link": "https://api.shoplightspeed.com/us/products/20974460/metafields.json"
// }
// },
// "reviews": {
// "resource": {
// "id": false,
// "url": "reviews?product=20974460",
// "link": "https://api.shoplightspeed.com/us/reviews.json?product=20974460"
// }
// },
// "type": false,
// "attributes": {
// "resource": {
// "id": false,
// "url": "products/20974460/attributes",
// "link": "https://api.shoplightspeed.com/us/products/20974460/attributes.json"
// }
// },
// "supplier": {
// "resource": {
// "id": 78794,
// "url": "suppliers/78794",
// "link": "https://api.shoplightspeed.com/us/suppliers/78794.json"
// }
// },
// "tags": {
// "resource": {
// "id": false,
// "url": "tags/products?product=20974460",
// "link": "https://api.shoplightspeed.com/us/tags/products.json?product=20974460"
// }
// },
// "variants": {
// "resource": {
// "id": false,
// "url": "variants?product=20974460",
// "link": "https://api.shoplightspeed.com/us/variants.json?product=20974460"
// }
// },
// "movements": {
// "resource": {
// "id": false,
// "url": "variants/movements?product=20974460",
// "link": "https://api.shoplightspeed.com/us/variants/movements.json?product=20974460"
// }
// }
// }
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
const productId = jResp.intOf('product.id');
const productCreatedAt = jResp.stringOf('product.createdAt');
const productUpdatedAt = jResp.stringOf('product.updatedAt');
const productVisibility = jResp.stringOf('product.visibility');
const productData01 = jResp.stringOf('product.data01');
const productData02 = jResp.stringOf('product.data02');
const productData03 = jResp.stringOf('product.data03');
const productUrl = jResp.stringOf('product.url');
const productTitle = jResp.stringOf('product.title');
const productFulltitle = jResp.stringOf('product.fulltitle');
const productDescription = jResp.stringOf('product.description');
const productContent = jResp.stringOf('product.content');
const productBrandResourceId = jResp.intOf('product.brand.resource.id');
const productBrandResourceUrl = jResp.stringOf('product.brand.resource.url');
const productBrandResourceLink = jResp.stringOf('product.brand.resource.link');
const productCategoriesResourceUrl = jResp.stringOf('product.categories.resource.url');
const productCategoriesResourceLink = jResp.stringOf('product.categories.resource.link');
const productDeliverydateResourceId = jResp.intOf('product.deliverydate.resource.id');
const productDeliverydateResourceUrl = jResp.stringOf('product.deliverydate.resource.url');
const productDeliverydateResourceLink = jResp.stringOf('product.deliverydate.resource.link');
const productRelationsResourceUrl = jResp.stringOf('product.relations.resource.url');
const productRelationsResourceLink = jResp.stringOf('product.relations.resource.link');
const productMetafieldsResourceUrl = jResp.stringOf('product.metafields.resource.url');
const productMetafieldsResourceLink = jResp.stringOf('product.metafields.resource.link');
const productReviewsResourceUrl = jResp.stringOf('product.reviews.resource.url');
const productReviewsResourceLink = jResp.stringOf('product.reviews.resource.link');
const productAttributesResourceUrl = jResp.stringOf('product.attributes.resource.url');
const productAttributesResourceLink = jResp.stringOf('product.attributes.resource.link');
const productSupplierResourceId = jResp.intOf('product.supplier.resource.id');
const productSupplierResourceUrl = jResp.stringOf('product.supplier.resource.url');
const productSupplierResourceLink = jResp.stringOf('product.supplier.resource.link');
const productTagsResourceUrl = jResp.stringOf('product.tags.resource.url');
const productTagsResourceLink = jResp.stringOf('product.tags.resource.link');
const productVariantsResourceUrl = jResp.stringOf('product.variants.resource.url');
const productVariantsResourceLink = jResp.stringOf('product.variants.resource.link');
const productMovementsResourceUrl = jResp.stringOf('product.movements.resource.url');
const productMovementsResourceLink = jResp.stringOf('product.movements.resource.link');
}