Medium is a platform that thrives on categorization through tags, helping readers discover content and enabling writers to expand their reach. Imagine you are writing an article about "technology" and want to explore closely related tags to improve its discoverability.
With Medium API, you can easily fetch related tags programmatically. Here's a step-by-step guide to do just that.
Before diving into the implementation, ensure you have the following:
dotenv
package for securely managing your API keys and the medium-api-js
package for interacting with Medium's API.npm install medium-api-js dotenv
.env
File: Place your API key in a .env
file to keep it secure. The file should look like this:RAPIDAPI_KEY=your_medium_api_key
The following code demonstrates how to fetch related tags for the "technology" tag using Medium's API.
import Medium from 'medium-api-js';
import dotenv from 'dotenv';
dotenv.config();
.env
file.dotenv.config()
, the environment variables (like your API key) become accessible in the code.const medium = new Medium(process.env.RAPIDAPI_KEY);
process.env.RAPIDAPI_KEY
: Fetches the API key securely from the .env
file.new Medium()
: Initializes the Medium API client with your API key.medium.getRelatedTags('technology')
.then(data => {
console.log('Related Tags:', data);
});
getRelatedTags('technology')
: Makes a request to fetch tags related to "technology.".then(data => {...})
: Handles the asynchronous response, logging the related tags.Save the code in a file named getRelatedTags.js
and run it in the terminal using:
node getRelatedTags.js
When the code runs successfully, the output will be:
given_tag
: The tag you queried, in this case, "technology."related_tags
: An array of tags that are closely related to the queried tag. These tags can be used to broaden the reach of your content.In this article, you learned how to fetch related tags for a specific Medium tag using the medium-api-js
library. This simple yet powerful functionality can help writers and developers optimize their content and build smarter tools. By incorporating related tags, you ensure that your articles reach a broader and more relevant audience.
If you have any questions about Medium API or the code, feel free to reach out to me at nishu@mediumapi.com. Happy coding!