Back

Reading and Writing Data Using the SharpSpring API

Aug 15, 2024 • 7 minute read

Hey there, fellow JavaScript devs! Ready to dive into the world of SharpSpring API integration? Let's get our hands dirty with some data syncing goodness for user-facing integrations. Buckle up!

Setting the Stage

SharpSpring's API is your ticket to seamless data flow between your app and their marketing automation platform. Whether you're building a custom integration or enhancing an existing one, mastering this API will make your life a whole lot easier.

Getting Started with SharpSpring API

Authentication: Your VIP Pass

First things first, let's get you authenticated. SharpSpring uses API keys and secrets. Here's a quick snippet to get you rolling:

const apiKey = 'your_api_key'; const secretKey = 'your_secret_key'; const accountId = 'your_account_id'; const headers = { 'Content-Type': 'application/json', 'X-SharpSpring-Account-ID': accountId };

API Endpoints: Your Data Highways

SharpSpring's main API endpoint is https://api.sharpspring.com/pubapi/v1/. Remember this URL; it's where all the magic happens!

Reading Data: Time to Fetch!

Grabbing Contacts

Let's fetch some contacts, shall we?

async function getContacts() { const response = await fetch('https://api.sharpspring.com/pubapi/v1/', { method: 'POST', headers, body: JSON.stringify({ method: 'getLeads', params: { where: {} }, id: 'getLeads' }) }); return await response.json(); }

Custom Fields: The Spice of Life

Need those custom fields? We've got you covered:

async function getCustomFields() { const response = await fetch('https://api.sharpspring.com/pubapi/v1/', { method: 'POST', headers, body: JSON.stringify({ method: 'getFields', params: { where: {} }, id: 'getFields' }) }); return await response.json(); }

Writing Data: Let's Create Some Magic!

Creating Contacts

Time to add some new folks to the mix:

async function createContact(contactData) { const response = await fetch('https://api.sharpspring.com/pubapi/v1/', { method: 'POST', headers, body: JSON.stringify({ method: 'createLeads', params: { objects: [contactData] }, id: 'createLeads' }) }); return await response.json(); }

Updating Contacts

People change, and so should their data:

async function updateContact(contactId, updateData) { const response = await fetch('https://api.sharpspring.com/pubapi/v1/', { method: 'POST', headers, body: JSON.stringify({ method: 'updateLeads', params: { objects: [{ id: contactId, ...updateData }] }, id: 'updateLeads' }) }); return await response.json(); }

Syncing Strategies: Keep It Fresh!

Incremental Sync

Why sync it all when you can sync what's new? Here's a basic incremental sync function:

async function incrementalSync(lastSyncTimestamp) { const newData = await getContacts({ where: { dateModified: { '>': lastSyncTimestamp } } }); // Process and update your local data with newData return new Date().toISOString(); // Return new timestamp for next sync }

Webhook Integration

For real-time updates, set up a webhook endpoint in your app:

app.post('/sharpspring-webhook', (req, res) => { const { event, data } = req.body; // Process the event and update your local data res.sendStatus(200); });

Optimizing API Usage: Work Smarter, Not Harder

Rate Limiting

SharpSpring has rate limits, so play nice:

const rateLimiter = new RateLimiter(100, 'minute'); async function apiCall(method, params) { await rateLimiter.removeTokens(1); // Make your API call here }

Caching

Cache frequently accessed data to reduce API calls:

const cache = new NodeCache({ stdTTL: 600 }); // 10-minute TTL async function getCachedContacts() { const cachedData = cache.get('contacts'); if (cachedData) return cachedData; const contacts = await getContacts(); cache.set('contacts', contacts); return contacts; }

Best Practices: The Cherry on Top

  1. Security: Always use HTTPS and keep your API keys secret.
  2. Error Handling: Implement robust error handling and retries for API calls.
  3. Data Integrity: Validate data before sending it to SharpSpring.
  4. Logging: Keep detailed logs for troubleshooting and auditing.

Wrapping Up

There you have it, folks! You're now armed with the knowledge to build a killer SharpSpring integration. Remember, the key to a great sync is consistency and efficiency. Keep your code clean, your syncs regular, and your users happy.

Now go forth and sync like a pro! 🚀