Medium is a popular platform for sharing ideas and stories, where tags help categorize and discover content. If you’re developing an app or conducting research, knowing how to programmatically search for tags can be invaluable.
By the end of this tutorial, you’ll know how to fetch tag search results dynamically using the medium-api-js
package and how to use this feature effectively.
To follow this guide, ensure you have the following:
dotenv
to manage API keys securely.medium-api-js
and dotenv
by running:npm install medium-api-js dotenv
Below is a breakdown of the code used to search for tags on Medium:
import Medium from 'medium-api-js';
import dotenv from 'dotenv';
dotenv.config();
medium-api-js
: A JavaScript wrapper for Medium’s API, simplifying API calls.dotenv
: A package to securely load environment variables from a .env
file.dotenv.config()
: Initializes the environment configuration so that sensitive details, such as the API key, are accessible.const medium = new Medium(process.env.RAPIDAPI_KEY);
process.env.RAPIDAPI_KEY
: Retrieves your Medium API key from the environment variables.new Medium()
: Creates a Medium API client instance with the key for authentication.medium.getSearchTags('coding').then(data => {
console.log('Search Results for Tags:', data);
});
medium.getSearchTags('coding')
: Sends a request to Medium’s API to fetch tags related to the query "coding"
..then(data => {...})
: Handles the API response. The fetched data is logged to the console.Save the code in a file (e.g., getSearchTags.js
) and run the script in the terminal with the following command:
node getSearchTags.js
Ensure that your .env
file contains the API key, formatted like this:
RAPIDAPI_KEY=your_api_key_here
When you execute the script, the console displays a structured object containing tags that match the query. For example:
tags
: An array of tag strings related to the query.search_query
: Echoes the original search term.For the query "coding"
, the output includes a comprehensive list of tags such as "coding-style"
, "coding-challenge"
, and "coding-for-beginners"
.
Searching for tags on Medium using medium-api-js
is a powerful way to explore and utilize content trends. By programmatically fetching tags, you can streamline workflows, discover new insights, and build better tools for Medium’s vibrant ecosystem.
If you have any questions, feel free to contact nishu@mediumapi.com.
Happy coding!