how send message facebook friend through graph api using Accessstoken

how send message facebook friend through graph api using Accessstoken

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Sending Messages via the Facebook Graph API: Beyond the /me/feed Endpoint

Attempting to send a message to a Facebook friend using the Graph API often leads developers down a path of confusion, especially when starting from the basic posting logic. A common initial approach involves calling the /me/feed endpoint with parameters like to=john and message. While this works perfectly for creating public status updates or posts on a wall, it will fail immediately if your goal is to deliver a private direct message (DM).

The core issue lies in understanding what endpoint handles which type of interaction. The /me/feed endpoint belongs to the Posts resource. When you post to this feed, the content becomes part of the user's public timeline unless specific permissions and settings are manipulated to restrict visibility, but it does not trigger the messaging infrastructure required for a private conversation. You cannot simply append to=friend_id to a feed post request expecting it to route through Messenger.

To send a message, you must target the Messages resource specifically. The endpoint is /me/messages. This endpoint expects a message object containing fields like recipient, which can be an array of user IDs or emails, and the actual text content under the text key.

Here is how the request structure should look in PHP:

$response = $facebook->call_api(
    '/me/messages', 
    'post', 
    [
        'access_token' => $token,
        'message' => [
            'recipient' => ['id' => 'john'], // Or an array for multiple recipients
            'text'     => 'You have a Test message'
        ]
    ]
);

Notice the shift from passing raw parameters directly in the query string to nesting them within a message object. The API validates that the recipient exists and is connected, or at least has been granted access via a valid permission flow.

The Critical Role of Permissions

Having an access token is only half the equation; the scope of that token dictates what actions are permissible. To write to the /me/messages endpoint, your application must have the messaging_pages or pages_messaging permission if acting on behalf of a Page, or simply the standard user permissions granted during login.

However, there is a significant limitation you must account for: User Consent.

If a user has not explicitly interacted with your app previously, Facebook's security model prevents them from receiving messages from unknown sources via API calls alone. You generally need to first engage the user through the "Send Friend Request" action or get them to like your Page/app. Once they have an established relationship (e.g., they are friends on Facebook), the API allows sending messages more freely. Without this prior connection, the request will likely return a 403 Forbidden error, regardless of how correct your payload is.

Handling Replies and Threads

Once the initial message is sent successfully, subsequent interactions usually occur within a specific thread. If you want to reply to a user's existing message, you do not send a new post; instead, you include the thread_id in your request parameters along with the new text. This ensures the reply appears in the correct conversation history rather than starting a disjointed chat stream.

$response = $facebook->call_api(
    '/me/messages', 
    'post', 
    [
        'access_token' => $token,
        'message' => [
            'recipient' => ['id' => 'john'],
            'text'      => 'Thanks for your reply!'
        ],
        'thread_id'    => '1234567890123456789' // The ID of the existing thread
    ]
);

Security and Best Practices

When handling access tokens, never expose them in frontend code or client-side scripts. Always perform API calls on your backend server where you can securely store and rotate these credentials. Passing a user's token from their browser to your backend to send messages is a viable pattern known as Server-Side Rendering (SSR), but it requires careful handling of token expiration and revocation.

If you are building a complex social application or need robust authentication flows, frameworks like Laravel simplify this process significantly. For instance, the LaravelCompany ecosystem often provides packages that abstract the OAuth 2.0 handshake and token management, reducing the boilerplate code needed to securely communicate with external APIs like Facebook's.

In summary, switching from the feed endpoint to the messages endpoint, structuring the payload correctly, and ensuring the necessary user relationships exist are the three pillars required to successfully send messages via the Graph API.

Our network