Back

Reading and Writing Data Using the SEMrush API

Aug 2, 20245 minute read

Hey there, fellow JavaScript wizards! Ready to dive into the world of SEMrush API integration? Let's get our hands dirty with some code and learn how to sync data like pros.

Setting Up the SEMrush API

First things first, let's get that API set up. You'll need an API key from SEMrush - guard it with your life (or at least don't commit it to public repos). Here's how you might initialize your API client:

const SEMrushAPI = require('semrush-api-client'); const client = new SEMrushAPI({ apiKey: process.env.SEMRUSH_API_KEY, baseUrl: 'https://api.semrush.com' });

Reading Data from SEMrush

Now that we're all set up, let's fetch some juicy data. The domain analytics endpoint is a goldmine. Check this out:

async function getDomainAnalytics(domain) { try { const response = await client.getDomainAnalytics(domain); return response.data; } catch (error) { console.error('Oops! Something went wrong:', error); } }

Writing Data to SEMrush

Sometimes we need to push data back to SEMrush. Updating project keywords? No sweat:

async function updateProjectKeywords(projectId, keywords) { try { await client.updateProjectKeywords(projectId, keywords); console.log('Keywords updated successfully!'); } catch (error) { console.error('Update failed:', error); } }

Implementing Data Sync

Syncing data efficiently is crucial. Here's a nifty function for incremental syncing:

async function incrementalSync(lastSyncDate) { const newData = await client.getUpdatedData(lastSyncDate); await saveToLocalDatabase(newData); return new Date(); // Return new sync date }

Error Handling and Retry Logic

APIs can be finicky. Let's add some retry logic with exponential backoff:

async function retryRequest(fn, maxRetries = 3) { for (let i = 0; i < maxRetries; i++) { try { return await fn(); } catch (error) { if (i === maxRetries - 1) throw error; await new Promise(r => setTimeout(r, 2 ** i * 1000)); } } }

Optimizing Performance

Batch operations can seriously speed things up. Here's how you might batch domain lookups:

async function batchDomainLookup(domains) { const chunks = chunkArray(domains, 100); // Split into chunks of 100 const results = await Promise.all( chunks.map(chunk => client.bulkDomainLookup(chunk)) ); return results.flat(); }

Webhooks and Real-time Updates

Stay on your toes with webhooks. Here's a simple Express handler:

app.post('/semrush-webhook', (req, res) => { const payload = req.body; // Process the webhook payload console.log('Received update:', payload); res.sendStatus(200); });

Testing and Debugging

Don't forget to test! Here's a quick Jest test to get you started:

jest.mock('semrush-api-client'); test('getDomainAnalytics returns correct data', async () => { const mockData = { organic: { traffic: 1000 } }; SEMrushAPI.prototype.getDomainAnalytics.mockResolvedValue({ data: mockData }); const result = await getDomainAnalytics('example.com'); expect(result).toEqual(mockData); });

Wrapping Up

And there you have it! You're now armed with the knowledge to read, write, and sync data like a SEMrush API ninja. Remember to keep an eye on those rate limits, cache when you can, and always handle errors gracefully.

Keep coding, keep learning, and may your API calls always return 200 OK! 🚀