{
  "info": {
    "name": "QBO tutorial: Reviewing loans with AI",
    "description": "Have AI read a loan and its documents, answer a question you define in a message template, and save the answer as a message whose status and priority you can sort, filter and act on. This example checks a loan for a prepayment penalty.\n\nAudience: PowerUser. Concepts: ai, message, document, mortgage-servicing, power-user.\n\nSet \u0060accessKey\u0060 and \u0060secretKey\u0060 as current values (or in an environment); never save them in the collection. Run folder 1 once to create the sample data, then the tutorial\u0027s requests in order.",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  },
  "auth": {
    "type": "bearer",
    "bearer": [
      {
        "key": "token",
        "value": "{{token}}",
        "type": "string"
      }
    ]
  },
  "event": [
    {
      "listen": "prerequest",
      "script": {
        "type": "text/javascript",
        "exec": [
          "// Exchange accessKey/secretKey for a bearer token, reused until a minute before it expires.",
          "const expires = Number(pm.collectionVariables.get(\u0027tokenExpires\u0027) || 0);",
          "if (Date.now() \u003E expires - 60000) {",
          "  pm.sendRequest({",
          "    url: pm.collectionVariables.get(\u0027baseUrl\u0027) \u002B pm.collectionVariables.get(\u0027corePath\u0027) \u002B \u0027/security/account/token\u0027,",
          "    method: \u0027POST\u0027,",
          "    header: { \u0027Content-Type\u0027: \u0027application/json\u0027 },",
          "    body: { mode: \u0027raw\u0027, raw: JSON.stringify({ accessKey: pm.variables.get(\u0027accessKey\u0027), secretKey: pm.variables.get(\u0027secretKey\u0027) }) }",
          "  }, (err, res) =\u003E {",
          "    if (err || res.code !== 200) { console.error(\u0027Token request failed\u0027, err || res.code); return; }",
          "    let t = res.json(); if (typeof t === \u0027string\u0027) t = JSON.parse(t);",
          "    pm.collectionVariables.set(\u0027token\u0027, t.accessToken);",
          "    pm.collectionVariables.set(\u0027tokenExpires\u0027, Date.now() \u002B (t.expiresInSeconds || 300) * 1000);",
          "  });",
          "}"
        ]
      }
    }
  ],
  "variable": [
    {
      "key": "baseUrl",
      "value": "https://your-qbo-site.example",
      "description": "Your QBO site\u0027s address (a test or UAT site, not production)."
    },
    {
      "key": "corePath",
      "value": "/core"
    },
    {
      "key": "accessKey",
      "value": "",
      "description": "Your access key. Set as a current value only."
    },
    {
      "key": "secretKey",
      "value": "",
      "description": "Your secret key. Set as a current value only."
    },
    {
      "key": "agentName",
      "value": "api3-1",
      "description": "AI agent registered on the site."
    },
    {
      "key": "token",
      "value": ""
    },
    {
      "key": "tokenExpires",
      "value": "0"
    },
    {
      "key": "template",
      "value": "",
      "description": "ID of MessageTemplate qbo4-fintech-regression-ai-review-template"
    },
    {
      "key": "loan_ppp",
      "value": "",
      "description": "ID of Loan qbo4-fintech-regression-ai-review-loan-ppp"
    },
    {
      "key": "note_ppp",
      "value": "",
      "description": "ID of Attachment qbo4-fintech-regression-ai-review-note-ppp"
    },
    {
      "key": "loan_no_ppp",
      "value": "",
      "description": "ID of Loan qbo4-fintech-regression-ai-review-loan-no-ppp"
    },
    {
      "key": "note_no_ppp",
      "value": "",
      "description": "ID of Attachment qbo4-fintech-regression-ai-review-note-no-ppp"
    },
    {
      "key": "message",
      "value": "",
      "description": "MessageID from \u0022Run AI Review on the loan\u0022"
    }
  ],
  "item": [
    {
      "name": "1. Set up sample data",
      "description": "Setup package \u0060ai-review\u0060. Records are keyed by SubscriberID, so running this again updates them.",
      "item": [
        {
          "name": "Save MessageTemplate Tutorial: Prepayment Penalty Review",
          "request": {
            "method": "POST",
            "url": "{{baseUrl}}/api/messagetemplate/save?Output=Json",
            "body": {
              "mode": "urlencoded",
              "urlencoded": [
                {
                  "key": "SubscriberID",
                  "value": "qbo4-fintech-regression-ai-review-template",
                  "type": "text"
                },
                {
                  "key": "MessageTemplate",
                  "value": "Tutorial: Prepayment Penalty Review",
                  "type": "text"
                },
                {
                  "key": "ParentObject",
                  "value": "Loan",
                  "type": "text"
                },
                {
                  "key": "Subject",
                  "value": "Prepayment penalty review",
                  "type": "text"
                },
                {
                  "key": "BodyText",
                  "value": "You are a mortgage loan boarding specialist. Review the loan data and the attached loan documents to determine whether the loan has a prepayment penalty.\n\n1. Read the promissory note and any prepayment rider or disclosure.\n2. Look for language that charges the borrower a fee, premium or penalty for paying principal early, including its amount or formula and how long it applies.\n3. Ignore late charges and other fees that are not tied to prepayment.\n\nYour response will be stored in a QBO message. In the body, summarize which documents you reviewed, quote the relevant language, and state your conclusion.\n\nIf the loan has a prepayment penalty, set Status to \u0022PPP Eligible\u0022 and Priority to 1.\nOtherwise, set Status to \u0022PPP Ineligible\u0022 and Priority to 3.",
                  "type": "text"
                }
              ]
            },
            "description": "\u0060MessageTemplate/Save\u0060 upserts by SubscriberID."
          },
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "// qbo responses may be JSON-encoded strings; find the property anywhere in the response.",
                  "let body = pm.response.json(); if (typeof body === \u0027string\u0027) body = JSON.parse(body);",
                  "const find = (o, k) =\u003E { if (o \u0026\u0026 typeof o === \u0027object\u0027) for (const [n, v] of Object.entries(o)) {",
                  "  if (n.toLowerCase() === k.toLowerCase() \u0026\u0026 v !== null \u0026\u0026 typeof v !== \u0027object\u0027) return v;",
                  "  const f = find(v, k); if (f !== undefined) return f; } };",
                  "const id = find(body, \u0027MessageTemplateID\u0027);",
                  "pm.test(\u0027returns MessageTemplateID\u0027, () =\u003E pm.expect(id, \u0027MessageTemplateID\u0027).to.exist);",
                  "if (id) pm.collectionVariables.set(\u0027template\u0027, id); else pm.collectionVariables.unset(\u0027template\u0027);"
                ]
              }
            }
          ]
        },
        {
          "name": "Save Loan TUTORIAL-PPP-001",
          "request": {
            "method": "POST",
            "url": "{{baseUrl}}/api/loan/save?Output=Json",
            "body": {
              "mode": "urlencoded",
              "urlencoded": [
                {
                  "key": "SubscriberID",
                  "value": "qbo4-fintech-regression-ai-review-loan-ppp",
                  "type": "text"
                },
                {
                  "key": "Loan",
                  "value": "TUTORIAL-PPP-001",
                  "type": "text"
                }
              ]
            },
            "description": "\u0060Loan/Save\u0060 upserts by SubscriberID."
          },
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "// qbo responses may be JSON-encoded strings; find the property anywhere in the response.",
                  "let body = pm.response.json(); if (typeof body === \u0027string\u0027) body = JSON.parse(body);",
                  "const find = (o, k) =\u003E { if (o \u0026\u0026 typeof o === \u0027object\u0027) for (const [n, v] of Object.entries(o)) {",
                  "  if (n.toLowerCase() === k.toLowerCase() \u0026\u0026 v !== null \u0026\u0026 typeof v !== \u0027object\u0027) return v;",
                  "  const f = find(v, k); if (f !== undefined) return f; } };",
                  "const id = find(body, \u0027LoanID\u0027);",
                  "pm.test(\u0027returns LoanID\u0027, () =\u003E pm.expect(id, \u0027LoanID\u0027).to.exist);",
                  "if (id) pm.collectionVariables.set(\u0027loan_ppp\u0027, id); else pm.collectionVariables.unset(\u0027loan_ppp\u0027);"
                ]
              }
            }
          ]
        },
        {
          "name": "Find Attachment Promissory Note - TUTORIAL-PPP-001",
          "request": {
            "method": "GET",
            "url": "{{baseUrl}}/api/attachment/select?SubscriberID=qbo4-fintech-regression-ai-review-note-ppp\u0026Output=Json",
            "description": "Looks up the document by SubscriberID; the upload below is skipped if it exists."
          },
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "// qbo responses may be JSON-encoded strings; find the property anywhere in the response.",
                  "let body = pm.response.json(); if (typeof body === \u0027string\u0027) body = JSON.parse(body);",
                  "const find = (o, k) =\u003E { if (o \u0026\u0026 typeof o === \u0027object\u0027) for (const [n, v] of Object.entries(o)) {",
                  "  if (n.toLowerCase() === k.toLowerCase() \u0026\u0026 v !== null \u0026\u0026 typeof v !== \u0027object\u0027) return v;",
                  "  const f = find(v, k); if (f !== undefined) return f; } };",
                  "const id = find(body, \u0027AttachmentID\u0027);",
                  "// Not found is fine: the next request creates it.",
                  "if (id) pm.collectionVariables.set(\u0027note_ppp\u0027, id); else pm.collectionVariables.unset(\u0027note_ppp\u0027);"
                ]
              }
            }
          ]
        },
        {
          "name": "Upload Attachment Promissory Note - TUTORIAL-PPP-001",
          "request": {
            "method": "POST",
            "url": "{{baseUrl}}/api/attachment/upload?Output=Json",
            "body": {
              "mode": "formdata",
              "formdata": [
                {
                  "key": "SubscriberID",
                  "value": "qbo4-fintech-regression-ai-review-note-ppp",
                  "type": "text"
                },
                {
                  "key": "Attachment",
                  "value": "Promissory Note - TUTORIAL-PPP-001",
                  "type": "text"
                },
                {
                  "key": "Object",
                  "value": "Loan",
                  "type": "text"
                },
                {
                  "key": "ObjectID",
                  "value": "{{loan_ppp}}",
                  "type": "text"
                },
                {
                  "key": "file",
                  "type": "file",
                  "src": "setup/Promissory Note - TUTORIAL-PPP-001.pdf"
                }
              ]
            },
            "description": "Uploads \u0060setup/Promissory Note - TUTORIAL-PPP-001.pdf\u0060 from this package. Select the file again if Postman can\u0027t find it."
          },
          "event": [
            {
              "listen": "prerequest",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "if (pm.collectionVariables.get(\u0027note_ppp\u0027)) pm.execution.skipRequest();"
                ]
              }
            },
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "// qbo responses may be JSON-encoded strings; find the property anywhere in the response.",
                  "let body = pm.response.json(); if (typeof body === \u0027string\u0027) body = JSON.parse(body);",
                  "const find = (o, k) =\u003E { if (o \u0026\u0026 typeof o === \u0027object\u0027) for (const [n, v] of Object.entries(o)) {",
                  "  if (n.toLowerCase() === k.toLowerCase() \u0026\u0026 v !== null \u0026\u0026 typeof v !== \u0027object\u0027) return v;",
                  "  const f = find(v, k); if (f !== undefined) return f; } };",
                  "const id = find(body, \u0027AttachmentID\u0027);",
                  "pm.test(\u0027returns AttachmentID\u0027, () =\u003E pm.expect(id, \u0027AttachmentID\u0027).to.exist);",
                  "if (id) pm.collectionVariables.set(\u0027note_ppp\u0027, id); else pm.collectionVariables.unset(\u0027note_ppp\u0027);"
                ]
              }
            }
          ]
        },
        {
          "name": "Save Loan TUTORIAL-PPP-002",
          "request": {
            "method": "POST",
            "url": "{{baseUrl}}/api/loan/save?Output=Json",
            "body": {
              "mode": "urlencoded",
              "urlencoded": [
                {
                  "key": "SubscriberID",
                  "value": "qbo4-fintech-regression-ai-review-loan-no-ppp",
                  "type": "text"
                },
                {
                  "key": "Loan",
                  "value": "TUTORIAL-PPP-002",
                  "type": "text"
                }
              ]
            },
            "description": "\u0060Loan/Save\u0060 upserts by SubscriberID."
          },
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "// qbo responses may be JSON-encoded strings; find the property anywhere in the response.",
                  "let body = pm.response.json(); if (typeof body === \u0027string\u0027) body = JSON.parse(body);",
                  "const find = (o, k) =\u003E { if (o \u0026\u0026 typeof o === \u0027object\u0027) for (const [n, v] of Object.entries(o)) {",
                  "  if (n.toLowerCase() === k.toLowerCase() \u0026\u0026 v !== null \u0026\u0026 typeof v !== \u0027object\u0027) return v;",
                  "  const f = find(v, k); if (f !== undefined) return f; } };",
                  "const id = find(body, \u0027LoanID\u0027);",
                  "pm.test(\u0027returns LoanID\u0027, () =\u003E pm.expect(id, \u0027LoanID\u0027).to.exist);",
                  "if (id) pm.collectionVariables.set(\u0027loan_no_ppp\u0027, id); else pm.collectionVariables.unset(\u0027loan_no_ppp\u0027);"
                ]
              }
            }
          ]
        },
        {
          "name": "Find Attachment Promissory Note - TUTORIAL-PPP-002",
          "request": {
            "method": "GET",
            "url": "{{baseUrl}}/api/attachment/select?SubscriberID=qbo4-fintech-regression-ai-review-note-no-ppp\u0026Output=Json",
            "description": "Looks up the document by SubscriberID; the upload below is skipped if it exists."
          },
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "// qbo responses may be JSON-encoded strings; find the property anywhere in the response.",
                  "let body = pm.response.json(); if (typeof body === \u0027string\u0027) body = JSON.parse(body);",
                  "const find = (o, k) =\u003E { if (o \u0026\u0026 typeof o === \u0027object\u0027) for (const [n, v] of Object.entries(o)) {",
                  "  if (n.toLowerCase() === k.toLowerCase() \u0026\u0026 v !== null \u0026\u0026 typeof v !== \u0027object\u0027) return v;",
                  "  const f = find(v, k); if (f !== undefined) return f; } };",
                  "const id = find(body, \u0027AttachmentID\u0027);",
                  "// Not found is fine: the next request creates it.",
                  "if (id) pm.collectionVariables.set(\u0027note_no_ppp\u0027, id); else pm.collectionVariables.unset(\u0027note_no_ppp\u0027);"
                ]
              }
            }
          ]
        },
        {
          "name": "Upload Attachment Promissory Note - TUTORIAL-PPP-002",
          "request": {
            "method": "POST",
            "url": "{{baseUrl}}/api/attachment/upload?Output=Json",
            "body": {
              "mode": "formdata",
              "formdata": [
                {
                  "key": "SubscriberID",
                  "value": "qbo4-fintech-regression-ai-review-note-no-ppp",
                  "type": "text"
                },
                {
                  "key": "Attachment",
                  "value": "Promissory Note - TUTORIAL-PPP-002",
                  "type": "text"
                },
                {
                  "key": "Object",
                  "value": "Loan",
                  "type": "text"
                },
                {
                  "key": "ObjectID",
                  "value": "{{loan_no_ppp}}",
                  "type": "text"
                },
                {
                  "key": "file",
                  "type": "file",
                  "src": "setup/Promissory Note - TUTORIAL-PPP-002.pdf"
                }
              ]
            },
            "description": "Uploads \u0060setup/Promissory Note - TUTORIAL-PPP-002.pdf\u0060 from this package. Select the file again if Postman can\u0027t find it."
          },
          "event": [
            {
              "listen": "prerequest",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "if (pm.collectionVariables.get(\u0027note_no_ppp\u0027)) pm.execution.skipRequest();"
                ]
              }
            },
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "// qbo responses may be JSON-encoded strings; find the property anywhere in the response.",
                  "let body = pm.response.json(); if (typeof body === \u0027string\u0027) body = JSON.parse(body);",
                  "const find = (o, k) =\u003E { if (o \u0026\u0026 typeof o === \u0027object\u0027) for (const [n, v] of Object.entries(o)) {",
                  "  if (n.toLowerCase() === k.toLowerCase() \u0026\u0026 v !== null \u0026\u0026 typeof v !== \u0027object\u0027) return v;",
                  "  const f = find(v, k); if (f !== undefined) return f; } };",
                  "const id = find(body, \u0027AttachmentID\u0027);",
                  "pm.test(\u0027returns AttachmentID\u0027, () =\u003E pm.expect(id, \u0027AttachmentID\u0027).to.exist);",
                  "if (id) pm.collectionVariables.set(\u0027note_no_ppp\u0027, id); else pm.collectionVariables.unset(\u0027note_no_ppp\u0027);"
                ]
              }
            }
          ]
        }
      ]
    },
    {
      "name": "2. Reviewing loans with AI",
      "item": [
        {
          "name": "Run AI Review on the loan",
          "request": {
            "method": "POST",
            "url": "{{baseUrl}}/api/loan/aireview?ID={{loan_ppp}}\u0026Template=Tutorial%3A%20Prepayment%20Penalty%20Review\u0026AttachmentIDs={{note_ppp}}\u0026AgentName={{agentName}}\u0026Output=Json",
            "description": "Sends the loan\u0027s summary, the template\u0027s instructions and the note to the AI agent, and saves its answer as a message. Takes up to a minute."
          },
          "event": [
            {
              "listen": "test",
              "script": {
                "type": "text/javascript",
                "exec": [
                  "// qbo responses may be JSON-encoded strings; find the property anywhere in the response.",
                  "let body = pm.response.json(); if (typeof body === \u0027string\u0027) body = JSON.parse(body);",
                  "const find = (o, k) =\u003E { if (o \u0026\u0026 typeof o === \u0027object\u0027) for (const [n, v] of Object.entries(o)) {",
                  "  if (n.toLowerCase() === k.toLowerCase() \u0026\u0026 v !== null \u0026\u0026 typeof v !== \u0027object\u0027) return v;",
                  "  const f = find(v, k); if (f !== undefined) return f; } };",
                  "const id = find(body, \u0027MessageID\u0027);",
                  "pm.test(\u0027returns MessageID\u0027, () =\u003E pm.expect(id, \u0027MessageID\u0027).to.exist);",
                  "if (id) pm.collectionVariables.set(\u0027message\u0027, id); else pm.collectionVariables.unset(\u0027message\u0027);"
                ]
              }
            }
          ]
        },
        {
          "name": "Read the AI Review message",
          "request": {
            "method": "GET",
            "url": "{{baseUrl}}/api/message/select?ID={{message}}\u0026Output=Json",
            "description": "The message\u0027s BodyText holds AI\u0027s findings; Status and Priority are what AI chose."
          },
          "event": []
        }
      ]
    }
  ]
}