Medium is a popular platform for sharing ideas and stories, and being a writer or editor in a publication can significantly enhance your reach.
In this guide, you’ll learn how to retrieve a list of Medium publications where a user is either a writer or an editor. This can be helpful for gaining insights and providing analystics on a user's engagement with different publications.
To follow this guide, ensure you have the following:
dotenv
package to manage environment variables securely.npm install medium-api-js dotenv
import Medium from 'medium-api-js';
import dotenv from 'dotenv';
dotenv.config();
Here:
medium-api-js
: A wrapper for the Medium API to simplify interactions.dotenv
: Loads environment variables from a .env
file, keeping your API keys secure.dotenv.config()
: Reads and applies variables defined in your .env
file.const medium = new Medium(process.env.RAPIDAPI_KEY);
The Medium
object is initialized with the API key retrieved from the .env
file. This key authenticates your requests to the Medium API.
const userId = '14d5c41e0264';
This is the Medium user ID for the account whose publications you want to retrieve. Replace this with the desired user’s ID.
medium.userPublications(userId)
.then(data => console.log('User Publications:', data));
Here:
userPublications(userId)
: Fetches the publications where the user is a writer or editor..then(data => ...)
: Logs the response data, which contains the publications' details.Save the code in a file, e.g., getUserPublications.js
. To run it, use the command:
node getUserPublications.js
When the code executes, it outputs the user’s publications, including their IDs and roles:
admin_in
: List of publication IDs where the user is an editor.writer_in
: List of publication IDs where the user is a writer.You can use these IDs to fetch more details about each publication or build tools to manage them.
Fetching publications where a user is a writer or editor is straightforward using the medium-api-js
package. With this information, you can gain more insights on a user's preferences and engagement with publications.
If you have any questions or need further assistance, feel free to reach out at nishu@mediumapi.com.
Happy coding!