For impatient folks here is a link to GitHub source to this project Notify Me Give a try and let me know how it goes.
Need for Weekend Project
Instacart is a unique platform where one can order their grocery needs from multiple local stores at once. Instacart shoppers then visit these shops in person to do all shopping as mentioned in the order and deliver to your home within an hour or so! For end-users, this is a really helpful service in the current situation (thank you COVID-19) where we all are restricted to stay home! Due to this, the site is currently experiencing tremendous demand and it’s almost getting difficult to place orders as Instacart Shoppers keep working on their backlog.
Since we are on Corona vacation aka House arrest, we’ve been trying to use online services as much as we can to get our essential needs addressed. Many local stores still don’t provide online shopping which makes using Instacart a necessity. But every time I’ve tried to place an order through their website to get items from stores such as Costco, Safeway or CVS, I’ve been presented with the message below. I was really annoyed and wanted to find out if there is anything I can do so that I will able to know right once delivery times are available on Instacart so that I can submit my order!
So I jotted down what I need to do that I can allow me to slack and jump to screen only when I have to place an order as per alert from my bot. Since then I’ve successfully created a small script that alerts me (via email or text) whenever Instacart has specific time windows available to submit order. And yes finally, I’ve been successful in placing an order on Instacart to get groceries from Costco and Foodmaxx. Below you will find how I came up with a solution and how you can use it for your own needs.
Instacart, Firefox and Postman
First I decided to find out which Instacart APIs being used to inform the user that delivery times are available or not. Earlier my friend from Nashville had mentioned to me that they were able to place orders from this website right away. So I set my delivery address to zip code 37067 (Franklin, Nashville, TN) to start with. It allowed me to see available times for Costco.
Firefox Developer Tools helped me to narrow down on two API calls which seem to be useful for my needs.
Then I switched to Postman to understand what are different inputs / outputs information provided by these APIs.
Out of this, I decided to use Instacart Retailer Delivery Information API for my cause as it not only indicates if delivery times are available for a given store or not, but it also provides exact day time details for each window (if available) which I may like to use in a future version of this bot!
Postman – My Delivery Guide
Before we dive further, one question that should come to mind is, how the heck I’m able to successfully run this API in Postman app though. Right? BTW if you don’t know Postman, it’s a Swiss army knife that every developer should have. It helps in understanding APIs real up close when you don’t have related documentation available!
Initially, I tried to use my Instacart credentials with BASIC AUTH in Postman but ran into a 401 (Unauthorized Access Error) message. I could able to locate session-id info in Firefox but setting up that info into Request Header didn’t help either. And that’s when I saw Cookies link in Postman as you can see in Retailer Info API Image. Manage Cookies section in Postman allows one to capture cookies from your Browser with help from Postman Chrome Inspector extension (available only on Chrome). With that, I’m able to access the above APIs through Postman, viola! We are ON to something great 🙂
Upon closer look at JSON output by Retailer Info API, I could see delivery related useful information in the modules section of this output. 1st dictionary from this section tells the browser if delivery times are available or not. If the types set as “icon_info”, then yes delivery windows are available for end-user to submit the order for that store. And if it has “error” as the value then it means Instacart Shoppers are super busy right now and we should try again later. Also when types set as “icon_info”, 2nd dictionary in this section provides all information on different time slot availability.Core Information to implement our Bot
Postman – Delivery guy comes to rescue again
And with all this information I was confident that I can start writing Python script with Requests module to retrieve relevant details and display/email/text me as necessary. But later on, I couldn’t able to figure out how to use these cookies with my beloved Requests module. Stuck again, but not for a long time though!
Another look at Postman revealed me Code link which was right there staring at me all this time. Eureka! Postman already generated the necessary code for me. Very next moment, I ran code snippet from Python – Requests section within my Jupyter Notebook. But to my despair that didn’t work either 🙁
I decided to give a try with the cURL code snippet from the command line and it did work as expected. It was a good sign and I could see light at the end of the tunnel! I was able to spot another python code snippet using http.client module. And this code did live up to my expectations and for the very first time, I was able to see API in real action based on given commands!
Perfect Setup using Mailgun, Twilio & Cronjob
Now that I’d basic working code at my disposal, I wanted to add all the bells and whistles that can really help me to get notifications without gluing me to computer all the time.
First thing I did was to register myself with Mailgun and Twilio to allow me to send emails / texts through my program. As I’ve never used both these services before I was bit skeptical for quick turnaround. Mailgun setup was super easy and straight forward. Within few minutes I was able to send emails through program. (Free version allows upto 5000 emails per month I guess, which is more than sufficient for my needs). In case of Twilio, I’d to bit play around to figure out how to make it work. Couple of Issues that I need to find a fix are:
- Error –
The phone number you are using to send a message is not activatedI need to use the phone number provided by Twilio instead of my own! Adding anew line character to message content Getting around with the pre-appended message “Sent from your Twilio trial account -“
And with that I started spamming myself with all text messages that no one sent me before! Next I integrated both these services into my main program. Also I enhanced script further to handle errors, read different credentials via dot-files and also made sure that one can use this script with different stores available in Instacart and user can change store list through configuration.
With all this working together, I setup cron job and went to sleep. At 2 am, I got a text and email mentioning Costco and Total Wine and More have delivery windows available to submit my orders. Since I’d already put everything in my cart, I was able to submit my order right away! Yay!!! Now I can continue to stay home and wait for Instacart Shopper (thank you buddy!) to deliver food/groceries right at our door step.
Since then I’ve made more changes so that script is more generic and available for anyone to use with minimal setup efforts. Here is the link to the Notify Me project on Github. Give a try and let me know if it’s being useful to you. Thank you for staying with me so far.