Skip to main content

Manage Friends

Learn how to manage friend relationships using the API.

Prerequisites

  • Bellamy Book backend running
  • Two user accounts
  • Authentication tokens

Step 1: Search for Users

Find users to add as friends:

const response = await fetch('http://localhost:5000/api/friends/search?q=john&limit=20', {
headers: {
'Authorization': `Bearer ${token}`
}
});

const users = await response.json();

Step 2: Send Friend Request

Send a friend request to another user:

const userId = 'target-user-id';

const response = await fetch('http://localhost:5000/api/Friendship/send-request', {
method: 'POST',
headers: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json' },
body: JSON.stringify({ toUserId: userId, message: 'Hi!' })
});
const result = await response.json();

Step 3: Check Friend Requests

View pending friend requests:

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

const requests = await response.json();


Step 4: Accept Friend Request

Accept a pending friend request:

const userId = 'user-who-sent-request';

const requestId = 123; // from pending-requests
const response = await fetch('http://localhost:5000/api/Friendship/accept-request', {
method: 'POST',
headers: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json' },
body: JSON.stringify({ friendshipId: requestId })
});

const result = await response.json();

Step 5: Get Friends List

View your friends:

const response = await fetch('http://localhost:5000/api/Friendship/friends?page=1&pageSize=20', {
headers: {
'Authorization': `Bearer ${token}`
}
});

const friends = await response.json();


Step 6: Get Mutual Friends

Find mutual friends with another user:

const userId = 'other-user-id';

const response = await fetch(`http://localhost:5000/api/Friendship/mutual-friends?userId2=${userId}`, {
headers: {
'Authorization': `Bearer ${token}`
}
});

const mutualFriends = await response.json();

Complete Example

async function manageFriends() {
const token = 'your-auth-token';

// 1. Search for users (api/friends search)
const searchResponse = await fetch('http://localhost:5000/api/friends/search?q=john', {
headers: { 'Authorization': `Bearer ${token}` }
});
const { data: users } = await searchResponse.json();

// 2. Send friend request
if (users.length > 0) {
const targetUser = users[0];
await fetch('http://localhost:5000/api/Friendship/send-request', {
method: 'POST',
headers: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json' },
body: JSON.stringify({ toUserId: targetUser.id, message: 'Hi!' })
});
}

// 3. Get friends
const friendsResponse = await fetch('http://localhost:5000/api/Friendship/friends?page=1&pageSize=20', {
headers: { 'Authorization': `Bearer ${token}` }
});
const { data: friends } = await friendsResponse.json();

}

manageFriends();

Friend Request Status

When viewing a user profile, check friend status:

  • null: Not friends, no request
  • "pending_sent": You sent a request
  • "pending_received": They sent a request
  • "friends": Already friends

Next Steps