This is a simple Node.js script that automates posting photos to Instagram using the instagram-private-api library. It logs into an Instagram account and posts a specified photo with a caption.
- Login to Instagram: Uses credentials to log into an Instagram account.
- Post Photos: Uploads photos with a caption to the logged-in account.
- Multiple Posts: Can be configured to post multiple photos in a loop.
- Node.js
- npm (Node Package Manager)
instagram-private-apilibrary
-
Clone the repository:
git clone https://github.com/reehazshrestha/InstagramBot-Js.git cd InstagramBot-Js -
Install the required dependencies:
npm install instagram-private-api
-
Update the script with your Instagram credentials.
- Set your Instagram username and password in the script.
- Specify the photo path and caption in the
postPhotofunction. - Run the script:
node index.js
const { IgApiClient } = require('instagram-private-api');
async function postPhoto() {
const ig = new IgApiClient();
ig.state.generateDevice('username');
try {
await ig.account.login('username', 'password');
} catch (error) {
console.error('Login failed:', error.message);
return;
}
const path = 'Dog-Coding.jpg';
const caption = 'JavaScript Bot';
try {
const publishResult = await ig.publish.photo({
file: path,
caption: caption,
});
console.log('Photo posted successfully:', publishResult);
} catch (error) {
console.error('Failed to post photo:', error.message);
}
}
async function executePosts() {
for (let i = 0; i < 10; i++) {
await postPhoto();
console.log(`Post attempt ${i + 1} completed.`);
}
}
executePosts().catch(console.error);postPhoto: Handles logging in and posting a photo with a caption.executePosts: CallspostPhotomultiple times to automate posting.
Feel free to submit issues or pull requests if you have suggestions or improvements!
Using bots to interact with Instagram may violate Instagram's Terms of Service. Use this script responsibly and at your own risk.