# Use and adapt Instagram-Meta-Business-API-Handler

Use the documented interfaces and source layout for **Instagram-Meta-Business-API-Handler**. The sections below retain the README’s examples and configuration details.

## Authentication

### Exchange Code for Token

```typescript
// After user authorizes your app, exchange code for token
const authResponse = await InstagramHandler.exchangeCodeForToken(
  'YOUR_CLIENT_ID',
  'YOUR_CLIENT_SECRET',
  'YOUR_REDIRECT_URI',
  'AUTHORIZATION_CODE'
);

console.log('Access Token:', authResponse.access_token);
```

### Get Long-Lived Token (60 days)

```typescript
const longToken = await InstagramHandler.getLongLivedToken(
  'YOUR_CLIENT_SECRET',
  'SHORT_LIVED_TOKEN'
);

console.log('Long-lived token:', longToken.access_token);
console.log('Expires in:', longToken.expires_in, 'seconds');
```

### Refresh Access Token

```typescript
const refreshed = await instagram.refreshAccessToken();
console.log('New token:', refreshed.access_token);
```

## Error Handling

```typescript
try {
  const mediaId = await instagram.postImage(url, caption);
  console.log('Posted successfully:', mediaId);
} catch (error) {
  console.error('Failed to post:', error.message);
  // Handle error appropriately
}
```

## Troubleshooting

**Token expired?**
```typescript
// Refresh your token
const newToken = await instagram.refreshAccessToken();
```

**Video not publishing?**
- Videos take time to process (2-10 seconds)
- The handler automatically waits, but ensure video meets requirements

**Comments not appearing?**
- Check if comments are enabled on the post
- Verify you have `instagram_manage_comments` permission

**DMs not working?**
- Ensure you have `instagram_manage_messages` permission
- User must have interacted with your account first

**Insights not available?**
- Insights require Instagram Business or Creator account
- Wait 24 hours for insights on new posts

## Source and help

- [GitHub repository](https://github.com/Up-to-code/Instagram-Meta-Business-API-Handler)
- [Original README](https://github.com/Up-to-code/Instagram-Meta-Business-API-Handler/blob/main/README.md)
- [Issues and existing reports](https://github.com/Up-to-code/Instagram-Meta-Business-API-Handler/issues)
