Shopify / shopify-node-app

An example app that uses Polaris components and shopify-express
MIT License
328 stars 136 forks source link

Correct way to make new api calls? #123

Closed pjatx closed 5 years ago

pjatx commented 6 years ago

Hey Guys,

A bit of a node N00B and trying to set up a simple carrier service that will provide different shipping rates to our wholesale customers.

To accomplish this, I need to loop through the items in the cart and check to see if see if their tags array contains a specific tag. I've been really struggling with making API calls within a new post route I've set up, mainly because I'm not sure where to grab the access token and it's not super clear in the docs (or maybe just my n00bness).

What's the correct way to set up a backend API properly call using the shop credentials? Currently doing something like this, but both shopDomain and accessToken are undefined. Have also tried different flavors of grabbing it from the session without luck. Any help would be appreciated!

request.session.accessToken req.session.accessToken

app.post('/rates', jsonParser, function (request, res) {

  const shopify = new ShopifyAPIClient({ shopName: shopDomain, accessToken: accessToken });
  shopify.product.get(1234)

  // Do more stufff

});

Ended up going the private route, but still curious on the correct way to do this.

Avidexp commented 6 years ago

I've been looking for a similar answer for a while. Let me know if you find out something! I can get it hitting my custom URL for rates, but it never renders the response of my api as valid rates to choose from...

pjatx commented 6 years ago

@Avidexp Did you register the carrier service first?

https://help.shopify.com/en/api/reference/shipping_and_fulfillment/carrierservice#create

Avidexp commented 6 years ago

Yes sir I did,

I ended up getting it to work. I had my rates in just an array, not an array wrapped in an object.

Ex.

[{ 'service_name': 'Globaltranz Fast', 'description': 'fastest rate', 'service_code': 'GTZ', 'total_price': '100000.80', 'currency': 'USD', 'min_delivery_date': '2018-07-28 14:48:45 -0400', 'max_delivery_date': '2018-07-30 14:48:45 -0400' }

rather than { "rates":[{ 'service_name': 'Globaltranz Fast', 'description': 'fastest rate', 'service_code': 'GTZ', 'total_price': '100000.80', 'currency': 'USD', 'min_delivery_date': '2018-07-28 14:48:45 -0400', 'max_delivery_date': '2018-07-30 14:48:45 -0400' } }

On Thu, Jul 26, 2018 at 12:45 PM, Philip Johnson notifications@github.com wrote:

@Avidexp https://github.com/Avidexp Did you register the carrier service first?

https://help.shopify.com/en/api/reference/shipping_and_ fulfillment/carrierservice#create

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Shopify/shopify-node-app/issues/123#issuecomment-408213443, or mute the thread https://github.com/notifications/unsubscribe-auth/ARVdLUQWZxMCclE25KPn9llCBLsPJE3Jks5uKhxDgaJpZM4U1Q5L .

pjatx commented 6 years ago

Ah the little things : ) Did you by chance use this library? Curious to see what your code looks like. I ended up writing mine with the node express library instead as a private app vs a public one.

Avidexp commented 6 years ago

So I actually made a public one doing the same thing with express lol. I found it was easier to eliminate some of the noise, and just try to build it out one step at a time, rather than using the template.

I can create/edit/delete carrier services no problem now, and I can pass back static rate information that gets procesed by shopify so the user can pick the rate. The only problem I'm having now, is I cant find the shopping cart information in the requests I get from shopify lol.

I'm using simple express API like: app.get('/shopify/callback', (req, res) => {}

Yet req.body always comes out empty. Have you experienced this as well?

pjatx commented 6 years ago

I can't quite remember now, but I can show you what I'm doing to loop through the cart items. I'd recommend using bodyParser.

...
// Set vars and imports etc...
...

const bodyParser = require('body-parser');

// Create application/json parser
var jsonParser = bodyParser.json()

// Shipping Carrier Service
app.post('/rates', jsonParser, function (req, res) {

...
/// rates logic here
...

  // Save rates request
  var ratesReq = req.body;
  var products = ratesReq['rate']['items']

});
Avidexp commented 6 years ago

bodyParser!!

Can't believe I forgot that! You're amazing pjatx, thanks man!!

pjatx commented 6 years ago

No worries man! Glad I could be helpful. I beat my head on this project for a long time : )

vmundhra commented 6 years ago

@pjatx

Apparently the session containing the accessToken, is present only in requests from the app UI client. i.e. React app in this case. Also the routes used are /api.

I am too struggling to understand how an online store code can make authenticated API requests to the app. Now I know this needs to be done using APP Proxy. I am able to hit my endpoint, verify the hmac validation so as to be sure that request was proxied through Shopify, but after that I am stuck. I am not understanding how to create a new ShopifyAPIClient like below

const shopify = new ShopifyAPIClient({ shopName: shopDomain, accessToken: accessToken });

If I try to create a client using

const client = new ShopifyAPIClient({ shopName: 'your-shop-name', apiKey: 'your-private-app-key', password: 'your-private-app-pwd' })

it becomes a private app and the apiKey and password need to be the private app created in Store admin and not of the App itself. Which makes it useless as it cannot be installed in different stores. I logged issue requests in this repos, shopify-node-app, shopify-express, shopify-api-node, but nobody seems to be helping.

Going crazy!

nwtn commented 5 years ago

Closing because this project is now deprecated.