BAMF.ai·
quickstart with bamf node client
if you're diving into the bamf node client, you’re in for a smooth ride. this example shows how to leverage native fetch to connect your environment with the bamf API, making your workflow efficient.
first things first, set up your environment variables. ensure your
BAMF_API_KEY is defined, or you'll hit a wall:
if (!API_KEY) throw new Error('Set BAMF_API_KEY first');.
this is your key to interact with the API.
here’s a straightforward async function, bamf, that prepares and fetches data from the API. it automatically handles authorization and responses. very handy.
```js
async function bamf(path, options = {}) {
const response = await fetch(${API_BASE}${path}, {
...options,
headers: {
'Content-Type': 'application/json',
Authorization: Bearer ${API_KEY},
...(options.headers || {}),
},
});
const body = await response.json().catch(() => ({}));
if (!response.ok) throw new Error(${response.status} ${JSON.stringify(body)});
return body;
}
```
now, let’s see how to fetch your authenticated user info and their associated creator spaces. you get a clean log of your user id and key type so you can confirm you’re connected:
```js
const me = await bamf('/me');
console.log('Authenticated as user', me.user_id, 'with', me.key_type, 'auth');
```
then, you can get a list of creator spaces:
```js
const spaces = await bamf('/creator-spaces');
const creator = spaces.data?.[0];
if (!creator) throw new Error('No creator spaces available');
```
if you’ve got a creator space, this code snippet fetches the scheduled posts, allowing you to manage your content effectively. it's all about making sure you’re on top of what’s coming up:
```js
const scheduled = await bamf(/posts?creator_id=${creator.id}&status=scheduled&include=media,first_comment,preview);
console.log('Scheduled posts', scheduled.data?.length || 0);
```
keep this snippet in your toolkit for quick reference when interacting with the bamf API.
for additional capabilities, check out the related posts API documentation for full CRUD operations on your content.
originally from [BAMF.ai](https://bamf.ai/docs/examples/node-client).