Quickstart guide: Search for products

node v10.24.1
version: 2.0.0
require("axios"); var sdk = require("ordercloud-javascript-sdk"); var ADMIN_USERNAME = "ADMIN_USERNAME"; var ADMIN_PASSWORD = "ADMIN_PASSWORD"; var CLIENT_ID = "YOUR_SHARED_CLIENT_ID"; // the default configuration interacts with our production environment // however you're most likely following this guide from our sandbox environment sdk.Configuration.Set({ baseApiUrl: "https://sandboxapi.ordercloud.io", }); // log in as your admin user var adminAuth = await sdk.Auth.Login( ADMIN_USERNAME, ADMIN_PASSWORD, CLIENT_ID, ["FullAccess"] ); // set your access token, the sdk will automatically append it to all subsequent requests sdk.Tokens.SetAccessToken(adminAuth.access_token); // create your first buyer organization var buyer = await sdk.Buyers.Create({ ID: "winterfell", Name: "Winterfell Inc.", Active: true, }); // create a security profile for your buyer organization var buyerSecurityProfile = await sdk.SecurityProfiles.Create({ ID: "buyersecurityprofile", Name: "Buyer Security Profile", Roles: ["Shopper"], }); // assign the security profile to your buyer organization await sdk.SecurityProfiles.SaveAssignment({ BuyerID: buyer.ID, SecurityProfileID: buyerSecurityProfile.ID, }); // create a user in your buyer organization var userPassword = "youKnowNothing1quot;; var user = await sdk.Users.Create(buyer.ID, { Username: "myuser", FirstName: "Jon", LastName: "Snow", Email: "jon@winterfell.com", Password: userPassword, Active: true, }); // Create a price schedule (product price) var priceSchedule = await sdk.PriceSchedules.Create({ ID: "simpleprice", Name: "Simple Price Schedule", MinQuantity: 1, PriceBreaks: [ { Quantity: 1, Price: 800, }, ], }); // Create a product and set the previously created price on it var product = await sdk.Products.Create({ ID: "product1", Name: "Longclaw Sword", DefaultPriceScheduleID: priceSchedule.ID, Active: true, }); await sdk.Catalogs.SaveProductAssignment({ ProductID: product.ID, // when a buyer is created with no DefaultCatalogID set, a catalog is made for that buyer with the same ID CatalogID: buyer.ID, }); // get a token for the buyer user you created var authResponse = await sdk.Auth.Login( user.Username, userPassword, CLIENT_ID, ["Shopper"] ); var buyerToken = authResponse.access_token; sdk.Tokens.SetAccessToken(buyerToken); // list products as that buyer after some delay so OrderCloud can finish indexing products await new Promise((resolve) => setTimeout(resolve, 5000)); var myProducts = await sdk.Me.ListProducts(); var myFirstProduct = myProducts.Items[0]; console.log( `User ${user.FirstName} ${user.LastName} can see product ${myFirstProduct.Name} listed for ${myFirstProduct.PriceSchedule.PriceBreaks[0].Price}` );

no comments

    sign in to comment