Developers

Search the docs

Search guides and headings.

Show meals for an address and a date

Write a command-line program that lists the vendors that deliver to a place, their next delivery dates, and the meals with prices on one date.

View as Markdown

You will write a command-line program. You give it a city, a province and a country, and optionally a date. It prints the vendors that deliver there, the next delivery dates of each vendor, and the meals each vendor offers on one date, with their prices.

Before you start#

  • You finished Set up your project. Your project has the nurishd client file and a Sandbox key in NURISHD_API_KEY.
  • Your key has the scope catalog:read. Every call in this tutorial needs it. Run check_key from the setup tutorial to see your scopes.
  • You need no extra package.

Take the city, the province and the country from the customer's delivery address. The street and the postal code are not used for this lookup. You send the whole address later, when you place the order.

The place lookup takes a city, a province and a country. It does not take a postal code. The province and the country are two capital letters, such as ON and CA. The city must match what the vendor stored, letter for letter. Toronto does not find a zone stored as toronto.

How the calls fit together#

You make one call to find the zones. Then you make a few calls for each vendor.

  1. GET /meals/v1/zone/query/documents finds the zones that serve the place. A zone is a delivery area of one vendor. It covers a city, a province or a whole country. You get the best zone of each vendor that delivers there.
  2. GET /meals/v1/vendor/{vendor_id}/get/document gives the name of the vendor and its minimum order.
  3. GET /meals/v1/vendor/{vendor_id}/zone/{zone_id}/delivery-date/query/documents lists the dates the zone delivers on.
  4. GET /meals/v1/vendor/{vendor_id}/zone/{zone_id}/delivery-date/{date}/get/document says which products you can order for one date. It gives product ids only.
  5. GET /meals/v1/vendor/{vendor_id}/product/query/documents lists the products of the vendor with their names and prices. One call gives up to 50 products. This is fewer calls than reading each product on its own.

That is four calls for each vendor, plus one for the zones.

1. Format money#

Prices are whole numbers of cents. A price of 1299 is 12.99. The currency is the zone_currency of the zone you order in. A product or a vendor has no currency of its own.

The function money turns cents and a currency into text.

python
def money(cents, currency):
    return f"{cents / 100:.2f} {currency}"

2. Read the vendor#

The zone answer holds the id of the vendor. The name is on the vendor. The vendor also has a minimum order: the smallest order value it takes, in cents. An order below it is refused with order_limits_not_met. The field is optional, so the code checks for it.

python
def print_vendor(vendor_id, currency):
    vendor = call("GET", f"/meals/v1/vendor/{vendor_id}/get/document")
    print()
    print(vendor["vendor_name"])
    minimum = vendor.get("vendor_minimum_order_value")
    if minimum is not None:
        print(f"  Minimum order: {money(minimum, currency)}")

3. List the delivery dates#

A delivery date is a day the zone delivers on. The call needs from and to as YYYY-MM-DD. They can be at most 60 days apart. This program asks for the next 14 days.

A date is listed only when the zone delivers on that weekday, ordering for it is still open, a menu is in production and it is not a blackout date. A blackout date is a day the vendor does not deliver.

Each value looks like 2026-04-07T04:00:00Z. It is midnight at the start of the delivery day in the vendor's timezone, written in UTC. The part before the T is the delivery day. This YYYY-MM-DD date is what the next call needs, and what you send later when you place an order. The code keeps the first ten characters of each value.

python
def delivery_days(zone_path):
    today = date.today()
    answer = call(
        "GET",
        f"{zone_path}/delivery-date/query/documents",
        {"from": today.isoformat(), "to": (today + timedelta(days=14)).isoformat()},
    )
    return [value[:10] for value in answer["valid_delivery_dates"]]

4. Read one date and list its meals#

The date answer has the order_cutoff_date. The cutoff is the last moment you can order for that delivery date, in UTC. It also has all_unique_product_ids, the ids of every product you can order for that date.

The ids have no names or prices. The product list has them. The code reads the vendor's products page by page with the pages helper and prints the ones whose id is in the answer.

python
def print_meals(vendor_id, zone_path, day, currency):
    offer = call("GET", f"{zone_path}/delivery-date/{day}/get/document")
    offered = set(offer["all_unique_product_ids"])
    print(f"  Meals on {day} (order before {offer['order_cutoff_date']}):")
    for product in pages(f"/meals/v1/vendor/{vendor_id}/product/query/documents"):
        if product["product_id"] in offered:
            print(f"    {product['product_name']}: {money(product['product_price'], currency)}")

5. Join the steps for one vendor#

The function showVendor runs steps 2 to 4 for one zone. It prints the first five dates. It uses the date you gave, or the first date when you gave none. It says so when the vendor does not deliver on your date.

python
def show_vendor(zone, wanted_date):
    vendor_id = zone["vendor"]
    zone_path = f"/meals/v1/vendor/{vendor_id}/zone/{zone['zone_id']}"
    currency = zone["zone_currency"]

    print_vendor(vendor_id, currency)
    days = delivery_days(zone_path)
    if not days:
        print("  No delivery dates in the next 14 days.")
        return
    print("  Next delivery dates: " + ", ".join(days[:5]))

    day = wanted_date or days[0]
    if day not in days:
        print(f"  Does not deliver on {day}.")
        return
    print_meals(vendor_id, zone_path, day, currency)

6. Find the vendors for a place#

The main function reads the place from the command line, finds the zones and shows each vendor. The code makes the province and the country capital letters for you. It prints a message when no vendor delivers there.

In the Production environment you see no vendors until Nurish'd enables them for your company. See Catalog.

python
def main():
    if len(sys.argv) < 4:
        print("Usage: python meals.py CITY PROVINCE COUNTRY [YYYY-MM-DD]")
        sys.exit(1)
    city = sys.argv[1]
    state = sys.argv[2].upper()
    country = sys.argv[3].upper()
    wanted_date = sys.argv[4] if len(sys.argv) > 4 else None

    zones = pages(
        "/meals/v1/zone/query/documents",
        {"city": city, "state": state, "country": country},
    )
    found = False
    for zone in zones:
        found = True
        show_vendor(zone, wanted_date)
    if not found:
        print(f"No vendor delivers to {city}, {state}, {country}.")

7. Run it#

Create the file for your language with the code from the whole program at the end of this page. Run it with a place.

LanguageCommand
Pythonpython meals.py Mississauga ON CA
JavaScriptnode meals.js Mississauga ON CA
TypeScriptnode meals.ts Mississauga ON CA
Javamvn -q compile exec:java -Dexec.mainClass=tutorial.Meals -Dexec.args="Mississauga ON CA"

Add a date at the end to choose the day. Use a date from the list the program printed. For example python meals.py Mississauga ON CA 2026-04-10.

You see one block for each vendor.

text
Harbour Kitchen
  Minimum order: 50.00 CAD
  Next delivery dates: 2026-04-07, 2026-04-10, 2026-04-14, 2026-04-17
  Meals on 2026-04-07 (order before 2026-04-04T19:30:00Z):
    Salmon Bowl: 14.95 CAD

Your dates and meals depend on the vendors and the day you run it.

Turn it into a page of your site#

Do not call the catalog from the browser. The browser would need your key. Add a route to your own server instead, such as GET /meals?city=Mississauga&state=ON&country=CA. It runs the same calls and returns the result as JSON. Your page reads that JSON.

The catalog changes slowly. Keep each answer in memory for a few minutes and serve it again to the next visitor. Each vendor costs four calls, and the Sandbox allows 25 calls a minute by default. See Rate limits.

The whole program#

python
# file: meals.py
import sys
from datetime import date, timedelta

from nurishd import NurishdError, call, pages


def money(cents, currency):
    return f"{cents / 100:.2f} {currency}"


def print_vendor(vendor_id, currency):
    vendor = call("GET", f"/meals/v1/vendor/{vendor_id}/get/document")
    print()
    print(vendor["vendor_name"])
    minimum = vendor.get("vendor_minimum_order_value")
    if minimum is not None:
        print(f"  Minimum order: {money(minimum, currency)}")


def delivery_days(zone_path):
    today = date.today()
    answer = call(
        "GET",
        f"{zone_path}/delivery-date/query/documents",
        {"from": today.isoformat(), "to": (today + timedelta(days=14)).isoformat()},
    )
    return [value[:10] for value in answer["valid_delivery_dates"]]


def print_meals(vendor_id, zone_path, day, currency):
    offer = call("GET", f"{zone_path}/delivery-date/{day}/get/document")
    offered = set(offer["all_unique_product_ids"])
    print(f"  Meals on {day} (order before {offer['order_cutoff_date']}):")
    for product in pages(f"/meals/v1/vendor/{vendor_id}/product/query/documents"):
        if product["product_id"] in offered:
            print(f"    {product['product_name']}: {money(product['product_price'], currency)}")


def show_vendor(zone, wanted_date):
    vendor_id = zone["vendor"]
    zone_path = f"/meals/v1/vendor/{vendor_id}/zone/{zone['zone_id']}"
    currency = zone["zone_currency"]

    print_vendor(vendor_id, currency)
    days = delivery_days(zone_path)
    if not days:
        print("  No delivery dates in the next 14 days.")
        return
    print("  Next delivery dates: " + ", ".join(days[:5]))

    day = wanted_date or days[0]
    if day not in days:
        print(f"  Does not deliver on {day}.")
        return
    print_meals(vendor_id, zone_path, day, currency)


def main():
    if len(sys.argv) < 4:
        print("Usage: python meals.py CITY PROVINCE COUNTRY [YYYY-MM-DD]")
        sys.exit(1)
    city = sys.argv[1]
    state = sys.argv[2].upper()
    country = sys.argv[3].upper()
    wanted_date = sys.argv[4] if len(sys.argv) > 4 else None

    zones = pages(
        "/meals/v1/zone/query/documents",
        {"city": city, "state": state, "country": country},
    )
    found = False
    for zone in zones:
        found = True
        show_vendor(zone, wanted_date)
    if not found:
        print(f"No vendor delivers to {city}, {state}, {country}.")


try:
    main()
except NurishdError as error:
    print(error)
    sys.exit(1)

If something goes wrong#

What you seeWhyWhat to do
No vendor delivers to ...No zone matches the place. Letter case counts in the city.Check the spelling of the city. Try the province or the country the zone covers.
403 scope_not_grantedYour key lacks catalog:read.Create a key with the scope.
422 validation_errorThe province or the country is not two letters, or the city has $, { or }.Use codes such as ON and CA.
404 not_found on a vendor or a zoneThe vendor is not live, or it is not enabled for your company in Production.Ask Nurish'd to enable the vendor.
Does not deliver on <date>The zone does not offer that date. The cutoff has passed, or no menu is ready.Choose a date from the list.
429 rate_limitedYou went over the calls allowed each minute.The client waits once and tries again. Wait for the minute to pass.

Next#