TPF Javascript to access EBay API

Viewing 3 reply threads
  • Author
    Posts
  • July 6, 2025 at 7:28 AM #52610

    Mark Moran
    Participant

    I’ve written this javascript to go out and grab sold ebay items and store then in my database. I can’t seem to get it to work. ChatGPT says it because: “Tap Forms JavaScript scripting engine does not have direct HTTP request capability (like fetch or XMLHttpRequest). Scripts are sandboxed and cannot reach out to the web directly.”

    Is this true? Any suggestions on how to get this down if this is the case? I’d like to use the script on my Mac and iOS devices.

    // Define Field IDs from the current form
    const coinTypeFieldId = ‘fld-7b49d2da923f4e90a22c2c43d476504b’;
    const yearFieldId = ‘fld-2196446daf504b619016cf97e293fa00’;
    const mintmarkFieldId = ‘fld-81ea772bc0e24626977c0d2ddece998e’;

    // Defind Field IDs for the Coins on Ebay form
    const Ebay_coin_type_id = ‘fld-1dcc89f4289e482c93cbc940fbe7ca09’;
    const Ebay_mint_mark_id = ‘fld-1e874b9e81c94622837943a57654a563’;
    const Ebay_year_id = ‘fld-2c3a9b6921084f909c69ac90b42335ea’;
    const Ebay_price_id = ‘fld-20c6a1a5d6a74538acccd39458d8c61e’;
    const Ebay_photo_id = ‘fld-e86d4deab234444db94a4b06e731e919’;
    const Ebay_web_site_id = ‘fld-d77c51322b99468193c94ba823fca06b’;

    // Define the Forms
    const coinsWantedForm = document.getFormNamed(‘Coins Wanted’);
    const coinsOnEbayForm = document.getFormNamed(‘Coins on eBay’);

    // Get the current record
    //const currentRecord = form.getRecord();

    // Get the coin search details directly from the record
    const coinType = record.getFieldValue(coinTypeFieldId);
    const year = record.getFieldValue(yearFieldId);
    const mintmark = record.getFieldValue(mintmarkFieldId);

    // eBay API Setup
    const EBAY_AUTH_TOKEN = ‘API TOKEN REDACTED’;

    const query = ${coinType} ${year} ${mintmark};

    // eBay Search URL
    const url = https://api.ebay.com/buy/browse/v1/item_summary/search?q=Lincoln%20Penny%201909%20S&filter=sold_status:SOLD;

    console.log(Searching eBay for: ${query});

    // Async Fetch Wrapper for Tap Forms
    async function fetchEbayData() {
    try {
    const response = await fetch(url, {
    method: ‘GET’,
    headers: {
    ‘Authorization’: Bearer ${EBAY_AUTH_TOKEN},
    ‘Content-Type’: ‘application/json’
    }
    });

    if (!response.ok) {
    throw new Error(HTTP Error: ${response.status});
    }

    const data = await response.json();

    if (!data.itemSummaries || data.itemSummaries.length === 0) {
    console.log(‘No eBay items found.’);
    return;
    }

    // Create Coins Wanted Record
    const wantedRecord = coinsWantedForm.addNewRecord();
    wantedRecord.setFieldValue(coinTypeFieldId, coinType);
    wantedRecord.setFieldValue(yearFieldId, year);
    wantedRecord.setFieldValue(mintmarkFieldId, mintmark);

    // Prepare link field array
    const linkedEbayRecords = [];

    // Process each eBay item
    for (let item of data.itemSummaries) {
    const ebayRecord = coinsOnEbayForm.addNewRecord();
    ebayRecord.setFieldValue(Ebay_coin_type_id, coinType)
    ebayReocrd.setFieldValue(Ebay_year_id, year)
    ebayRecord.setFieldValue(Ebay_mint_mark_id, mintmark)
    ebayRecord.setFieldValue(Ebay_price_id, item.price.value);
    ebayRecord.setFieldValue(Ebay_web_site_id, item.itemWebUrl);

    // Save all available photos
    let allPhotos = [];

    // Add the main image
    if (item.image && item.image.imageUrl) {
    allPhotos.push(item.image.imageUrl);
    }

    // Add additional images if available
    if (item.additionalImages && item.additionalImages.length > 0) {
    item.additionalImages.forEach(img => {
    if (img.imageUrl) {
    allPhotos.push(img.imageUrl);
    }
    });
    }

    // Save all photos to the Photo field (must be a Photo field that accepts multiple items)
    ebayRecord.setFieldValue(Ebay_photo_id, allPhotos);

    // Save all photos (only saves the featured photo)
    // if (item.image && item.image.imageUrl) {
    // ebayRecord.setFieldValue(‘fld-ebay-photo-id’, [item.image.imageUrl]);
    // }

    linkedEbayRecords.push(ebayRecord);
    }

    // Link eBay records to Coins Wanted
    wantedRecord.setFieldValue(‘Coins on Ebay’, linkedEbayRecords);

    console.log(Found and linked ${linkedEbayRecords.length} eBay items.);

    } catch (error) {
    console.error(‘eBay API Error:’, error);
    }
    }

    // Start the async function
    fetchEbayData();

    July 6, 2025 at 11:34 AM #52611

    Daniel Leu
    Participant

    I assume you’re using Tap Forms Pro. Well, ChatGPT isn’t aware of all the API features. You can certainly do different HTTP access’, but you got to use the TFP API.

    You should be able to use Utils.makeHTTPRequest(url, body, headers, method) instead of fetch() with const body = '';

    • This reply was modified 11 hours, 55 minutes ago by Daniel Leu.

    Cheers, Daniel
    ---
    See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks

    July 6, 2025 at 10:24 PM #52620

    Brendan
    Keymaster

    I find ChatGPT very useful sometimes. But often it’s just plain wrong or I guess assumes that because it doesn’t know, then it’s not possible to do something.

    Take a look in the Utils area of the JavaScript API documentation. There’s lots of good stuff in there that Tap Forms can do:

    https://www.tapforms.com/help-mac/pro/en/topic/javascript-api

    July 6, 2025 at 10:24 PM #52621

    Brendan
    Keymaster

    I find ChatGPT very useful sometimes. But often it’s just plain wrong or I guess assumes that because it doesn’t know, then it’s not possible to do something.

    Take a look in the Utils area of the JavaScript API documentation. There’s lots of good stuff in there that Tap Forms can do:

    https://www.tapforms.com/help-mac/pro/en/topic/javascript-api

Viewing 3 reply threads

You must be logged in to reply to this topic.