An unofficial Medium.com API package that allows interaction with Medium's features using Node.js.
## Install
To install the `@apiclient.xyz/medium` package, ensure you have Node.js and npm installed on your system. Then, run the following command in your terminal:
```bash
npm install @apiclient.xyz/medium
```
## Usage
This guide will walk you through how to use the `@apiclient.xyz/medium` package to interact with the Medium API. This includes steps to initialize your Medium account, retrieve account and publication information, and create new posts. The examples will use ESM syntax and TypeScript to ensure you have the best experience with code intelligence and type safety.
### 1. Importing Required Classes
Start by importing the necessary classes from the `@apiclient.xyz/medium` package.
```typescript
import { MediumAccount, MediumPublication, MediumPost, IPostData } from '@apiclient.xyz/medium';
```
### 2. Initializing a Medium Account
To use the Medium API, you'll need to initialize a `MediumAccount` object with your access token.
```typescript
const mediumAccount = new MediumAccount('YOUR_ACCESS_TOKEN_HERE');
// Wait for the account to be ready
await mediumAccount.readyDeferred.promise;
```
### 3. Fetching Account Information
Retrieve account information such as ID, username, URL, and image URL.
You can interact with publications associated with your account by fetching all publications, getting publications you own, and fetching publications by their names.
Effective error handling is essential when interacting with APIs. Make sure to wrap your asynchronous code in try/catch blocks to handle any rejected promises.
```typescript
try {
const result = await mediumAccount.getAccountInfo();
console.log(result);
} catch (error) {
console.error('Failed to fetch account information:', error);
}
```
### Complete Example
Here is a complete example script that demonstrates the major functionalities of the `@apiclient.xyz/medium` package.
```typescript
import { MediumAccount, IPostData } from '@apiclient.xyz/medium';
async function run() {
const mediumAccount = new MediumAccount('YOUR_ACCESS_TOKEN_HERE');
Depending on the use case, you might need to fetch posts by specific criteria, update posts, or delete them. For this example, let's assume updating and deleting posts functions are supported by the Medium API, even though those capabilities aren't documented here.
By following this guide, you should be able to leverage the extensive features provided by the `@apiclient.xyz/medium` package. From account management to creating and managing posts within publications, this guide offers the complete scenarios you need in order to interact effectively with the Medium API using Node.js and TypeScript.
### Extensions
Consider creating more complex interactions by combining different endpoint requests. For example:
- Creating a scheduled post by integrating with a scheduling library like node-schedule.
- Fetching and analyzing post stats if such endpoints are provided by Medium.
- Automating cross-platform publication by combining Medium's API with other platforms' APIs.
The flexibility and power of Node.js along with the typed safety of TypeScript provide a robust environment for developing applications that interact with web APIs efficiently and effectively.
Start by running the complete example script to get familiar with the basic functionalities. Build upon these examples to create feature-rich applications that make extensive use of Medium's capabilities.
Always ensure to consult Medium's API documentation for any updates or additional features that may be available for further enhancement and optimization of your integrations.