Listening webhooks in Python

I have created a flask app and trying to get webhook data in one of the URL’s in that app but not able to figure out how to listen webhook properly and successfully. Could you please guide how to listen webhook properly in python under flask if possible.
Following contains one of the method created in flask app to read tote complete data through webhook.

@app.route(’/Tote_Complete’, methods=[‘POST’])
def totocomp():
if request.method == ‘POST’:
file2write=open(“Tote_Complete_jsondata”,‘a’)
file2write.write(request.json)
file2write.close()
return ‘success’, 200
else:
abort(400)

This particular flask app is deployed on cloud is successfully getting requests when sent manually but doesn’t get data from shiphero.
Please guide through how to listen webhook successfully

Hello @RohitArora.202 and welcome to the ShipHero Public API Community!

Could you share with me the account you are working with or the email and which it was created?

Kind regards,
TomasFD

Hi @tomasfd , The account I have been working is created on the email address as follows -
jake@shiplabsd.com

Hello @RohitArora.202!

The webhook hasn’t triggered yet, have you done full MIB and SIB picks that you think should’ve triggered the webhook?

Have a nice day!
TOmasFD

Hi @tomasfd ,
Please enlighten regarding the MIB and SIB picks have to performed from where and how?

Moreover I wanted to know the webhooks are correctly created? I am working on 4 different webhooks (Tote Complete , Tote Cleared , Order Packed , Package Added) and just wanted to know as suggested by the Account Holder(Jake) that concerned account is 3PL account and as mentioned in the documentation

“If you are using a 3PL account, webhooks need to be registered on the Customer account and not the 3PL account to trigger correctly.”

For this I tried to fetch ID for the customer in the 3PL account using the query mentioned in documentation and received a response that

“GraphQLError: Cannot query field ‘customers’ on type ‘AccountQueryResult’”

So just wanted to validate the correctness of webhooks paths and are they perfectly created?

Thank you

Hello @RohitArora.202!

They seem to be set up correctly. You can use the following query to recheck the URLs:

query{
  webhooks{
    request_id
    complexity
    data{
      edges{
        node{
          name
          shop_name
          url
        }
      }
    }
  }
}

I just updated the Docs. There was a mistake with the query, and this is the correct one:

query {
  account {
    data {
      customers {
        edges {
          node {
            id
          }
        }
      }
    }
  }
}

Best,
TomasFD

Hi Tomas, do these web hooks need to be registered at each client account or can we set them up at the 3PL account level? I believe they’re currently at the 3PL level so that could be why it’s not showing any?

Also, from your experience how often will the web hooks fail to trigger on ShipHero’s end (i.e. server overload)? We’re building this for reporting purposes, so it’s important that we capture the transaction information with a timestamp for all of these to accurately see how long certain tasks are taking in the warehouse.

Hello @jake_shiplab!

I’ve been running some tests on my end and there is something wrong with that webhook. Let me take the matter to our engineering team.

Are you using our API or just webhooks? There are 2 queries that might be of use to you: picks_per_day and packs_per_day. Let me know if you would like to dive deeper into these.

We do not have metrics on it. All I can tell you is, in my tests, which do not add up to what a real account manages in volume, I have not had any webhook not firing.

Have a great day,
TomasFD

Thanks for looking into it. The reason that we can’t just rely on picks_per_day and pack_per_day via the API is that it doesn’t offer much granularity to then drill down by client, actual hours worked on batches, etc.

For example, it would show that User A picked 200 pieces that day, but we can’t see the breakdown of that by client and then beyond that, we don’t know if User A worked a full 8 hours on picking. By using the webhooks with all the transactional information we can run queries on start/finish times of batches, clients, etc.

Hi @tomasfd ,

We are using both the methods to extract data i.e webhooks and API . We are extracting packs_per_day , picks_per_day , warehouse_products from shiphero database using API and Tote Complete, Tote Cleared , Order Packed , Package Added using webhook.

We are using python language to extract data both from API and webhook , we are able to extract through API but stuck with webhook.

As mentioned by you that webhooks are firing and the URL’s doesn’t seem to listen to them can you guide the right way to create the code to listen webhook in python, we are using flask framework to create decorators with different end points for 4 different webhook as mentioned above and this application is deployed on heroku cloud where it accepts POST requests. The snippet of one decorator(Tote complete) is mentioned below

@app.route('/Tote_Complete', methods=['POST'])
def totecomp():
    if request.method == 'POST':
        client = pymongo.MongoClient("mongodb+srv://**************:*********@cluster0.c2pfk.mongodb.net/webhooksdata?retryWrites=true&w=majority") #MongoDB code
        db = client.webhooksdata #MongoDB code 
        x= datetime.datetime.now() 
        testing=db['tote_complete'] #MongoDB code
        a=request.get_json() #getting post data from webhook 
        a=json.loads(a) #converting it into dictionary
        a['time_webhook']=str(x)
        testing.insert_one(a) #Inserting into mongodb database
        response = make_response(jsonify({"code": "200","Message": "Success"}),200) #Creating a json format reply
        response.headers['Content-Type']='application/json' #Setting header for response format
        return response
    else:
        response = make_response(jsonify({"code": "500","Status": "Failure"}),500)
        response.headers['Content-Type']='application/json'
        return response

This is just a basic code I created to get started with webhook listening , I tried doing manual POST requests on these decorator and they seem to work fine . Can you please validate the code mentioned above in concerned with shiphero webhook method.

Thank you

Hello @RohitArora.202!

The problem at the moment with the webhooks is that the servers are returning a 500 error. Have you tried querying your registered webhooks with our API to check the endpoint URLs are correct?

Kind regards,
TomasFD

Hi @tomasfd ,

Yes , I have cross checked the URL and they are correct , I have created that application that will listen to webhook from scratch that’s why I wanted some guidance over the python code as shared in the earlier response in there you can notice that we are using request module to GET data in return it’s sending a jsonified response for that. So please if you can guide over how exactly the shiphero webhook is expecting a response we can make it work then.

Moreover this application is made using flask web framework and deployed on heroku cloud platform.

Hello @RohitArora.202!

Unfortunately, we cannot help you with the code in your backend as we do not provide that kind of support here in the community…

What I can tell you is we expect a code 200 and a message like {'success': True} or {'code': '200', 'Status': 'Success'}

Kind regards,
TomasFD

Hi @tomasfd ,
We were able to make webhooks work and out of those 4 webhooks we are receiving data for only two of them i.e Tote Cleared and Order Packed Out
The other two doesn’t seem to work i.e Tote Complete and Package Added

Hi Tomas, it seems strange that the two webhook events are being received, but the most common ones, tote complete and package added are not triggering. We should have plenty of those events triggering each day in the account. Are you able to see if they are successfully firing on your end?

Hello @jake_shiplab and @RohitArora.202!

I’m looking into that now.

Have a nice day,
TomasFD

Hi @tomasfd ,
Any updates on the issue regarding tote complete and package added webhooks?

Hi @tomasfd, we had a call this morning to review the project and it seems like we’re just waiting on answers regarding these 2 web hooks not firing. Do you happen to have any updates for your team? Thanks so much

Hello @jake_shiplab!

I’m working first on the tote complete. I will update you tomorrow morning on the status of both.

Kind regards,
TomasFD

Hello @RohitArora.202

Could you re-register the “Tote Complete” webhook and test if you are receiving the payloads?

Kind regards,
TomasFD