Api to import ofx?

My bank allow to download daily transaction statements which I convert to ofx.

 

Now, is there a way to either via api tout import the ofx file like I the UI or at least be able to select multiple files when importing?

5replies Oldest first
  • Oldest first
  • Newest first
  • Active threads
  • Popular
  • Cyan Transistor - The API doesn't natively support uploading an OFX file but you could write something that parses the OFX file and hits the /budgets/{budget_id}/transactions endpoint to create/import the transactions.  Also, the web File-Based Import only supports one file at a time.  Hope this helps!

    Reply Like
  • Any chance of adding multiple files support for import or have more explicit api for import ? Especially since it seems api imported statements don't seem to be possible to setup to cleared nor automatically grouped.
     

    Reply Like
    • Cyan Transistor You could submit a Feature Request for the ability to upload multiple OFX files through File-based Import on the web application.

      As for importing the OFX files through the API, I threw together an example (below) using Node.js (JavaScript), using the ofx package to parse OFX files and import multiple files through the API.  This is crude and not fully tested but I was able to import an OFX using it and will hopefully be enough to get you going in the right direction.

      var fs = require("fs");
      var ofx = require("ofx");
      var ynab = require("ynab");
      
      const accessToken = "123-not-real-456";
      const targetYNABAccountId = "789-not-real-012";
      const sourceOfxFiles = ["bank-export-1.ofx", "bank-export-2.ofx"];
      
      function addTransactionsFromOfxFile(filePath, transactions) {
        const ofxRawData = fs.readFileSync(filePath, "utf8");
        const ofxParsed = ofx.parse(ofxRawData);
        const ofxTransactions = ofxParsed.OFX.BANKMSGSRSV1.STMTTRNRS.STMTRS.BANKTRANLIST.STMTTRN;
      
        for (let txn of ofxTransactions) {
          const id = txn.FITID;
          const dateISO = `${txn.DTPOSTED.substr(0, 4)}-${txn.DTPOSTED.substr(4,2)}-${txn.DTPOSTED.substr(2, 2)}`;
          const amountMilliunits = txn.TRNAMT * 1000;
          const memo = txn.MEMO;
      
          transactions.push({
            import:id: id,
            account_id: targetYNABAccountId,
            cleared: ynab.SaveTransaction.ClearedEnum.Cleared,
            approved: true,
            date: dateISO,
            amount: amountMilliunits,
            payee_name: memo
          });
        }
      }
      
      async function importTransactionsToYNAB(accessToken, transactions) {
        const ynabAPI = new ynab.API(accessToken);
        await ynabAPI.transactions.createTransactions("last-used", { transactions });
      }
      
      (async () => {
        const transactions = [];
        for (let ofxFile of sourceOfxFiles) {
          addTransactionsFromOfxFile(ofxFile, transactions);
        }
        await importTransactionsToYNAB(accessToken, transactions);
      })();
      
      Reply Like
  • Thanks! would these imported transactions get the payee rules applied ? and can they be cleared like file imported transaktions? 

    Reply Like
    • Cyan Transistor If an import_id is specified and a payee_name is specified, then yes, the payee rules will get applied.  See the "Model" area of the endpoint documentation for more details.  

      I've updated the example above to specify an import_id (using the FITID field from the OFX file) and also passing in the memo as the payee_name.  There a lots of variations for how data is represented on OFX files so you'll want to check your bank's file and make adjustments as necessary.

      Also, yes, they can be marked as cleared.  In the example above, I'm specifying

      cleared: ynab.SaveTransaction.ClearedEnum.Cleared

      will will mark them as cleared when imported.

      Reply Like
Like Follow
  • 3 mths agoLast active
  • 5Replies
  • 81Views
  • 2 Following