Simple JavaScript SDK to interact with Facebook API.
npm install facebook-js-sdk


A simple npm package to interact with Facebook API.
``bash`
npm install facebook-js-sdk
`javascript
const Facebook = require('facebook-js-sdk');
const fb = new Facebook({
appId: 'your-app-id',
appSecret: 'your-app-secret',
redirectUrl: 'your-redirect-url'
});
// Get login URL
const url = fb.getLoginUrl(['email', 'public_profile']);
// Exchange code for token
fb.callback('code-from-facebook')
.then(response => {
fb.setAccessToken(response.data.access_token);
});
// Make API calls
fb.get('/me')
.then(response => console.log(response.data));
`
`typescript
import Facebook from 'facebook-js-sdk';
interface UserProfile {
id: string;
name: string;
email?: string;
}
const fb = new Facebook({
appId: 'your-app-id',
appSecret: 'your-app-secret',
redirectUrl: 'your-redirect-url'
});
// Get login URL with type-safe permissions
const url = fb.getLoginUrl(['email', 'public_profile']);
// Exchange code for token with type safety
const response = await fb.callback('code-from-facebook');
fb.setAccessToken(response.data.access_token);
// Make API calls with type safety
const profile = await fb.get
console.log(profile.data.name);
`
`typescript`
new Facebook({
appId?: string;
appSecret?: string;
redirectUrl?: string;
graphVersion?: string; // defaults to 'v20.0'
accessToken?: string;
})
You must provide either:
- accessToken for direct API access, orappId
- , appSecret, and redirectUrl for OAuth flow
#### getLoginUrl(permissions: string[]): string
Generate a Facebook login URL with the specified permissions.
#### callback(code: string): Promise<{ access_token: string, ... }>
Exchange an OAuth code for an access token.
#### getAccessToken(): string | undefined
Get the current access token.
#### setAccessToken(accessToken: string): void
Set the access token for API calls.
#### get
Make a GET request to the Facebook API.
#### post
Make a POST request to the Facebook API.
#### delete
Make a DELETE request to the Facebook API.
`bashInstall dependencies
npm install
Please read CONTRIBUTING.md for details on our code of conduct and the process for submitting pull requests.
This project is licensed under the MIT License - see the LICENSE file for details.