# Shopify - Custom Function (New)

### 🛠️ How to Connect Shopify to Stammer.ai and Provide Tracking Info via AI Agent

In this guide, we’ll walk through how to set up a **custom function** in Stammer.ai that allows your chatbot to respond with tracking information based on a user’s email.

#### 🔍 Use Case Example:

**User:** Where’s my order?\
**Bot:** What’s your email?\
**User:** <name@email.com>\
**Bot:** Your order for the "product-name" has been fulfilled and delivered. It was shipped via USPS with the tracking number 920019030243561811486929. You can track your order here.

***

### Step 1: Create a Private App in Shopify

1. **Go to your Shopify Admin Panel**
2. Click **Apps and sales channels > App development**
3. Click **Create an app** or select an existing one
4. Name your app (e.g. `AI Order Tracking`)
5. Under **Configuration**, click **Edit** on Admin API access scopes
6. Enable the following scopes:

   ```
   read_orders  
   read_shipping  
   read_fulfillments  
   read_customers  
   ```
7. Click **Save**

***

### Step 2: Get the Admin API Access Token

1. Click on the **API credentials** tab in your app settings
2. Click **Install App** (if it’s not already installed)
3. Copy the **Admin API access token** (starts with `shpat_...`)
   * You will need this for your Stammer function
   * Store this securely

***

### Step 3: Set Up the Custom Function in Stammer.ai

1. Log in to your **Stammer.ai** account
2. Go to your **AI Agent > Functions** tab
3. Click **+ Add Function**

#### Fill in the following fields:

* **Function Alias:** `get_tracking_by_email`
* **Description:** Get order tracking data from Shopify
* **Function URL:**

  ```
  https://<your-store>.myshopify.com/admin/api/2023-04/orders.json
  ```

  Replace `<your-store>` with your actual Shopify subdomain
* **Method:** `GET`

#### Add a Parameter:

| Field       | Value        |
| ----------- | ------------ |
| Name        | `email`      |
| Type        | `String`     |
| Description | user’s email |
| Required    | ✅ Yes        |

***

### Step 4: Add Headers

Click **+ Add Header** and paste in your API access token as the value.

| Header Name              | Value                     |
| ------------------------ | ------------------------- |
| `X-Shopify-Access-Token` | `shpat_...` (from step 2) |

***

### Step 5: Add Static Parameters

Click **+ Add Parameter**

| Name   | Value |
| ------ | ----- |
| status | any   |

This makes sure the API includes all orders, regardless of fulfillment status.

***

### Step 6: Write a Prompt Instruction

Paste this in the **Prompt Instructions** field:

```
- If the user asks to track their order, you will ask for their email address 
- After you have required information call this get_tracking_by_email() function
```

***

### Step 7: Save and Test

1. Click **Test Function** and try using a test email of a recent order
2. If successful, the response will include order data like:
   * Customer name
   * Fulfillment status
   * Tracking number
   * Carrier

***

### 🧠 AI Agent Response Handling

Your AI agent should be instructed (via its system prompt) to:

* Ask for the user’s email
* Call the `get_tracking_by_email()` function

The AI agent will then return the available information to the user in the chat conversation. You can format this data to make it look any way you'd like by including an example in the base system prompt.

***

### Final Output Example:

**"Your order for the Nike Sneakers has been fulfilled and shipped via FedEx. Tracking number: 784569302145. Track it here: <https://www.fedex.com/fedextrack/?trknbr=784569302145>"**


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.stammer.ai/stammer.ai-docs/chat-ai-agents/functions-labs/custom-functions/shopify-custom-function-new.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
