A Sample Steemit API Endpoint for Location-Based Search

in Steem Devlast month

image_from_model_a (2).jpeg

The Steemit API provides a specific endpoint for searching content by location, called getDiscussionsByLocation. This endpoint allows you to query posts based on various location-based parameters.

The getDiscussionsByLocation method takes an object as its argument, with the following properties:

  • country (string): The country name.
  • state (string): The state or region name.
  • city (string): The city name.
  • radius (number): The search radius in kilometers.
  • start_author (string, optional): The starting author name for pagination.
  • start_permlink (string, optional): The starting permlink for pagination.
  • limit (number, optional): The maximum number of results to return (default is 10).

Here's an example of how to use this endpoint with the steem-js library:

const steem = require('steem-js');

// Set the location parameters
const locationParams = {
  country: 'United States',
  state: 'California',
  city: 'Los Angeles',
  radius: 50, // in kilometers
  limit: 20 // maximum number of results
};

// Use the `getDiscussionsByLocation` method to fetch posts
steem.api.getDiscussionsByLocation(locationParams, (err, result) => {
  if (err) {
    console.error(err);
  } else {
    console.log(result);
  }
});

This code will fetch up to 20 posts created within a 50-kilometer radius of Los Angeles, California, United States.

Pagination and Cursor-Based Queries

The getDiscussionsByLocation method supports cursor-based pagination, which means you can fetch additional results by providing the start_author and start_permlink parameters. This is useful when you want to retrieve more than the default limit of 10 results.

Here's an example of how to paginate through the results:

let startAuthor = '';
let startPermlink = '';

function fetchLocationPosts(locationParams) {
  locationParams.start_author = startAuthor;
  locationParams.start_permlink = startPermlink;

  steem.api.getDiscussionsByLocation(locationParams, (err, result) => {
    if (err) {
      console.error(err);
    } else {
      // Process the results
      console.log(result);

      // Update the pagination cursors
      startAuthor = result[result.length - 1].author;
      startPermlink = result[result.length - 1].permlink;

      // Recursively call the function to fetch the next page
      fetchLocationPosts(locationParams);
    }
  });
}

// Initial call with location parameters
fetchLocationPosts(locationParams);

This code will fetch the first page of results, then recursively call the fetchLocationPosts function with the updated start_author and start_permlink parameters to retrieve the next page of results.

Filtering and Sorting

In addition to the location-based parameters, you can also filter and sort the results using the following optional parameters:

  • tag (string): Filter by a specific tag.
  • sort (string): Sort the results. Possible values are trending, created, active, cashout, payout, and votes.
  • order (string): The sort order, either ascending or descending.

For example, to fetch the most recent posts within a 50-kilometer radius of Los Angeles, sorted by creation date in descending order:

const locationParams = {
  country: 'United States',
  state: 'California',
  city: 'Los Angeles',
  radius: 50,
  sort: 'created',
  order: 'descending',
  limit: 20
};

steem.api.getDiscussionsByLocation(locationParams, (err, result) => {
  if (err) {
    console.error(err);
  } else {
    console.log(result);
  }
});

By leveraging the Steemit API's location-based search capabilities, you can build applications and tools that help users discover relevant content and connect with their local communities on the Steemit platform.