top of page

Coding

import { fetch } from 'wix-fetch';

import wixData from 'wix-data';

 

async function getPetsFromPetfinder() {

const tokenRes = await fetch("https://api.petfinder.com/v2/oauth2/token", {

method: "POST",

headers: { "Content-Type": "application/x-www-form-urlencoded" },

body: "grant_type=client_credentials&client_id=TXD5gEHIDE1V92MB9YQYPlKl3WSWvvKBao20pB94xABndtwN0Y&client_secret=di1FoAaFjGvsNU8zCKuWurcYa57b33HNlivBCMmQ"

});

 

const tokenData = await tokenRes.json();

const token = tokenData.access_token;

 

const petsRes = await fetch("https://api.petfinder.com/v2/animals?organization=TXD5gEHIDE1V92MB9YQYPlKl3WSWvvKBao20pB94xABndtwN0Y", {

headers: { "Authorization": `Bearer ${token}` }

});

 

const pets = await petsRes.json();

 

for (let pet of pets.animals) {

await wixData.save("AdoptablePets", {

title: pet.name,

petfinderId: pet.id,

breed: pet.breeds.primary,

age: pet.age,

description: pet.description,

photo: pet.photos[0]?.medium || null

// Add more fields as needed

});

}

}

petfinder.jsw

import { fetch } from 'wix-fetch';

import wixData from 'wix-data';

 

const CLIENT_ID = 'TXD5gEHIDE1V92MB9YQYPlKl3WSWvvKBao20pB94xABndtwN0Y';

const CLIENT_SECRET = 'di1FoAaFjGvsNU8zCKuWurcYa57b33HNlivBCMmQ';

const ORG_ID = 'TXD5gEHIDE1V92MB9YQYPlKl3WSWvvKBao20pB94xABndtwN0Y';

 

export async function syncPetsFromPetfinder() {

// Step 1: Get Access Token

const tokenRes = await fetch("https://api.petfinder.com/v2/oauth2/token", {

method: "POST",

headers: {

"Content-Type": "application/x-www-form-urlencoded"

},

body: `grant_type=client_credentials&client_id=${CLIENT_ID}&client_secret=${CLIENT_SECRET}`

});

 

const tokenData = await tokenRes.json();

const accessToken = tokenData.access_token;

 

// Step 2: Get Pet Data

const petsRes = await fetch(`https://api.petfinder.com/v2/animals?organization=${ORG_ID}&limit=100`, {

headers: {

Authorization: `Bearer ${accessToken}`

}

});

 

const petsData = await petsRes.json();

const pets = petsData.animals;

 

// Step 3: Save or Update CMS

for (let pet of pets) {

await wixData.save("Pets", {

title: pet.name,

petfinderId: pet.id,

age: pet.age,

breed: pet.breeds.primary,

description: pet.description,

image: pet.photos[0]?.medium || null,

lastSynced: new Date()

});

}

 

return `${pets.length} pets synced.`;

}

jobs.config

{

"jobs": [

{

"functionLocation": "backend/petfinder.jsw",

"functionName": "syncPetsFromPetfinder",

"description": "Sync Petfinder pets daily",

"executionConfig": {

"cronExpression": "0 0 * * *", // Every day at midnight

"timezone": "America/Los_Angeles"

}

}

]

}

cropped-Header-Logo.png

Thanks

Petco_Love_Lost_Logo.jpg
Full-Color-Logo-For-Pets.-For-People.-For-Good.png
best+Friends+Socity+logo+Trans.png
bottom of page