Skip to main content

Create First Post (API)

This tutorial walks you through creating your first post using the Bellamy Book API. Follow each step in order; you can copy-paste the code into your browser console, a Node script, or Postman.

What you'll do

  1. Log in — get an auth token and your user ID.
  2. Create a text post — send a simple "Hello, world!" post.
  3. Optional: Create a post with an image, view the post, and load your feed.

By the end you'll have a post visible in the app and know how to call the Post API.


Prerequisites

  • Bellamy Book backend running at http://localhost:5000 (or your API base URL).
  • A user account (sign up via the app or register API).
  • Replace [email protected] and password in the examples with your credentials.

Step 1: Authenticate

Log in to get an access token and user ID. You need both for creating a post.

const loginResponse = await fetch('http://localhost:5000/api/Authentication/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
password: 'password'
})
});

const loginData = await loginResponse.json();
const token = loginData.data.token;
const userId = loginData.data.userId || loginData.data.user?.id; // use the field your API returns

Result: You now have token and userId. Use them in the next steps. If your login response uses different property names for the user ID, use that (e.g. loginData.data.id).


Step 2: Create a text post

Create a simple post with only text. The API expects multipart/form-data (or form-urlencoded) with caption and visibility.

const formData = new FormData();
formData.append('caption', 'Hello, world! This is my first post!');
formData.append('visibility', 'Public');

const response = await fetch(`http://localhost:5000/api/Post/users/${userId}`, {
method: 'POST',
headers: { 'Authorization': `Bearer ${token}` },
body: formData
});

const result = await response.json();

Result: result should contain the created post (e.g. result.data.id). You can open the app and see the post in your feed.


Step 3: Create a post with an image (optional)

To attach an image, add a file to the same endpoint using the mediaFiles field.

const formData = new FormData();
formData.append('caption', 'Check out this amazing photo!');
formData.append('visibility', 'Friends');
formData.append('mediaFiles', fileInput.files[0]); // from <input type="file">

const response = await fetch(`http://localhost:5000/api/Post/users/${userId}`, {
method: 'POST',
headers: { 'Authorization': `Bearer ${token}` },
body: formData
});

const post = await response.json();

Step 4: View your post

Retrieve a single post by ID (use the ID from the create response):

const postId = result.data.id; // from Step 2 or 3

const response = await fetch(`http://localhost:5000/api/Post/${postId}`, {
headers: { 'Authorization': `Bearer ${token}` }
});

const postData = await response.json();

Step 5: Get your feed

Load the home feed to see your post with others:

const response = await fetch(
`http://localhost:5000/api/Post/feed/home?userId=${userId}&limit=20&offset=0`,
{ headers: { 'Authorization': `Bearer ${token}` } }
);

const feed = await response.json();

Privacy options

Set visibility when creating a post:

ValueWho can see
PublicEveryone
FriendsOnly friends
only_meOnly you
customSpecific lists

Complete copy-paste example

Run this after replacing email and password. It logs in, creates one post, and logs the response.

async function createFirstPost() {
const baseUrl = 'http://localhost:5000';
const email = '[email protected]';
const password = 'password';

// 1. Login
const loginRes = await fetch(`${baseUrl}/api/Authentication/login`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email, password })
});
const loginData = await loginRes.json();
const token = loginData.data.token;
const userId = loginData.data.userId ?? loginData.data.user?.id ?? loginData.data.id;
if (!token || !userId) throw new Error('Login failed or missing token/userId');

// 2. Create post
const formData = new FormData();
formData.append('caption', 'Hello, world! My first post from the API.');
formData.append('visibility', 'Public');

const postRes = await fetch(`${baseUrl}/api/Post/users/${userId}`, {
method: 'POST',
headers: { 'Authorization': `Bearer ${token}` },
body: formData
});
const post = await postRes.json();
console.log('Created post:', post);
return post;
}

createFirstPost();

Next steps