Full Code With NodeJS and instagram-private-api
:
require("dotenv").config();
const { IgApiClient } = require("instagram-private-api");
const postToInsta = async (orderDetails) => {
try {
//Contect Instagram API Client
const ig = new IgApiClient();
ig.state.generateDevice(process.env.IG_USERNAME);
await ig.account.login(process.env.IG_USERNAME, process.env.IG_PASSWORD);
//Set user ID
const userId = await ig.user.getIdByUsername("said user");
const thread = ig.entity.directThread([userId.toString()]);
//Send Mesage
await thread.broadcastText("Message from node");
return true;
} catch (err) {
return err;
}
};
const instaPost = postToInsta().then(function (instaPost) {
if (instaPost == true) {
console.log(`Message Sent Succesfully`);
}
});
Just set your environment variables in your .env file using
IG_USERNAME = "<username>"
IG_PASSWORD = "<password>"
To Download
npm i instagram-private-api
Docs:
https://www.npmjs.com/package/instagram-private-api