Generating API for .txt Format

Overview

The .txt format provides a simple and straightforward way to handle lead data. Each line in the file represents a separate lead entry.

How to Create an API

  1. Set Up Your Endpoint: Create an endpoint in your application, for example: POST /api/leads/txt
  2. Fetch Data: Use your server-side logic to gather lead information.
  3. Format the Output: Structure the lead data into plain text, ensuring each lead is on a new line.
  4. Send Response: The API should send the formatted text file as a downloadable response.

Example Code Snippet (Node.js)

				
					app.post('/api/leads/txt', (req, res) => {
    const leads = getLeads(); // Function to fetch leads from your database
    const txtOutput = leads.map(lead => `${lead.name}, ${lead.email}`).join('\n');
    res.setHeader('Content-Type', 'text/plain');
    res.attachment('leads.txt');
    res.send(txtOutput);
});
				
			

Generating API for .xml Format

Overview

The .xml format allows you to provide structured data, making it easier to parse for various applications.

How to Create an API

  1. Set Up Your Endpoint: Create an endpoint like POST /api/leads/xml
  2. Fetch Data: Gather your lead data from the database.
  3. Format the Output as XML: Convert your data into XML format.
  4. Send Response: Respond with the XML content in the correct MIME type.

Example Code Snippet (Node.js)

				
					app.post('/api/leads/xml', (req, res) => {
    const leads = getLeads();
    const xmlOutput = `<leads>${leads.map(lead => `<lead><name>${lead.name}</name><email>${lead.email}</email></lead>`).join('')}</leads>`;
    res.setHeader('Content-Type', 'application/xml');
    res.attachment('leads.xml');
    res.send(xmlOutput);
});
				
			

Generating API for .xls Format

Overview

The .xls format is used for Microsoft Excel and is suitable for users who prefer to work with spreadsheets.

How to Create an API

  1. Set Up Your Endpoint: Create an endpoint like POST /api/leads/xls
  2. Fetch Data: Gather your leads from the database.
  3. Generate the Spreadsheet: Use a library like xlsjs to create the spreadsheet format.
  4. Send Response: Send the generated file as a response.

Example Code Snippet (Node.js)

				
					const XLSX = require('xlsx');

app.post('/api/leads/xls', (req, res) => {
    const leads = getLeads();
    const ws = XLSX.utils.json_to_sheet(leads);
    const wb = XLSX.utils.book_new();
    XLSX.utils.book_append_sheet(wb, ws, 'Leads');

    const xlsOutput = XLSX.write(wb, { bookType: 'xls', type: 'buffer' });
    res.setHeader('Content-Type', 'application/vnd.ms-excel');
    res.attachment('leads.xls');
    res.send(xlsOutput);
});
				
			

Generating API for .csv Format

Overview

The .csv format is widely supported and is easy to import into various applications.

How to Create an API

  1. Set Up Your Endpoint: Create an endpoint like POST /api/leads/csv
  2. Fetch Data: Retrieve the leads from your database.
  3. Format Data as CSV: Convert the data to CSV format.
  4. Send Response: Send the CSV data in the response.

Example Code Snippet (Node.js)

				
					app.post('/api/leads/csv', (req, res) => {
    const leads = getLeads();
    const csvOutput = leads.map(lead => `${lead.name},${lead.email}`).join('\n');
    res.setHeader('Content-Type', 'text/csv');
    res.attachment('leads.csv');
    res.send(csvOutput);
});
				
			

Generating API for .json Format

Overview

The .json format is highly versatile and widely used for API data transfer.

How to Create an API

  1. Set Up Your Endpoint: Create an endpoint like POST /api/leads/json
  2. Fetch Data: Retrieve leads from the database.
  3. Send Response in JSON: Directly respond with the lead data in JSON format.

Example Code Snippet (Node.js)

				
					app.post('/api/leads/json', (req, res) => {
    const leads = getLeads();
    res.setHeader('Content-Type', 'application/json');
    res.json(leads);
});