If you’re working with the Medium API to integrate Medium publications into your application, one of the key identifiers you’ll need is the publication ID. This unique identifier is essential for performing operations like fetching articles or analyzing publication data.
Let’s walk through how to fetch the publication ID for a given publication slug using the medium-api-js
package.
Before proceeding, ensure the following:
medium-api-js
Package: Install the package in your project by running:npm install medium-api-js
dotenv
Package: Install the package for managing environment variables:npm install dotenv
.env
file in your project’s root directory and store your RapidAPI key:RAPIDAPI_KEY=your_rapidapi_key_here
This script fetches the publication ID for a given Medium publication slug. Let’s break it down:
import Medium from 'medium-api-js';
import dotenv from 'dotenv';
dotenv.config();
Medium
: The primary class from medium-api-js
used to interact with the Medium API.dotenv
: Allows loading environment variables from a .env
file for secure and convenient API key management.dotenv.config()
: Ensures that variables from the .env
file are available throughout the script.const medium = new Medium(process.env.RAPIDAPI_KEY);
process.env.RAPIDAPI_KEY
: Accesses the API key securely stored in the .env
file.new Medium()
: Initializes the Medium client with the provided API key, enabling authenticated API calls.const publicationSlug = 'codex';
publicationSlug
: The slug of the publication whose ID you want to fetch. Replace 'codex'
with the slug of your target publication.medium.getPublicationId(publicationSlug).then(data => {
console.log('Publication ID:', data);
});
getPublicationId(publicationSlug)
: A method provided by medium-api-js
to fetch the publication ID based on the slug..then(data => { ... })
: Handles the promise returned by the API call. The data
object contains the publication ID and slug, which is logged to the console.Save the script to a file (e.g., getPublicationId.js
) and run it using Node.js:
node getPublicationId.js
When the script runs successfully, you’ll see an output similar to this:
publication_id
: The unique ID of the Medium publication.publication_slug
: The slug used to fetch the ID, confirming the correct mapping.In this tutorial, you learned how to fetch a Medium publication ID using the medium-api-js
package. This process enables seamless integration with Medium publications, allowing for enhanced functionalities like article retrieval and content analysis.
If you have any questions or need further assistance, feel free to reach out to me at nishu@mediumapi.com.