Creating Facebook Events using the Graph API is fairly simple. And here’s a small code to get you started:

 

The basic approach

<?php
$app_id = "APP_ID";
$app_secret = "APP_SECRET";
$my_url = "REDIRECT_URL";

$code = $_REQUEST["code"];

if(empty($code)) {
	$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
	. $app_id . "&redirect_uri=" . urlencode($my_url)
	. "&scope=create_event";
	echo("<script>top.location.href='" . $auth_url . "'</script>");
}

$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$access_token = file_get_contents($token_url);

$event_url = "https://graph.facebook.com/me/events?" . $access_token;
?>
<!doctype html>
<html>
<head>
<title>Create An Event</title>
<style>
label {float: left; width: 100px;}
input[type=text],textarea {width: 210px;}
</style>
</head>
<body>
<form enctype="multipart/form-data" action="<?php echo $event_url; ?>" method="post">
	<p><label for="name">Event Name</label><input type="text" name="name" value="" /></p>
	<p><label for="description">Event Description</label><textarea name="description"></textarea></p>
	<p><label for="location">Location</label><input type="text" name="location" value="" /></p>
	<p><label for="">Start Time</label><input type="text" name="start_time" value="<?php echo date('Y-m-d H:i:s'); ?>" /></p>
	<p><label for="end_time">End Time</label><input type="text" name="end_time" value="<?php echo date('Y-m-d H:i:s', mktime(0, 0, 0, date("m")  , date("d")+1, date("Y"))); ?>" /></p>
	<p><label for="picture">Event Picture</label><input type="file" name="picture" /></p>
	<p>
		<label for="privacy_type">Privacy</label>
		<input type="radio" name="privacy_type" value="OPEN" checked='checked'/>Open&nbsp;&nbsp;&nbsp;
		<input type="radio" name="privacy_type" value="CLOSED" />Closed&nbsp;&nbsp;&nbsp;
		<input type="radio" name="privacy_type" value="SECRET" />Secret&nbsp;&nbsp;&nbsp;
	</p>
	<p><input type="submit" value="Create Event" /></p>
</form>
</body>
</html>

Facebook Event FormOur Facebook event form

Explanation

Here we are:

  1. Getting the user access_token
  2. Preparing the Graph API link we are posting to $event_url
  3. And finally, displaying the form with content type “multipart/form-data” for the picture upload

Results

If everything went fine, you should receive the newly created event id:

{
   "id": "XXXXXXXXXXXXX"
}

Facebook eventsOur event published on Facebook

Back to top
 

Using the PHP-SDK

$nextWeek = time() + (7 * 24 * 60 * 60);
$event_param = array(
    "access_token" => "XXXXXXXX",
    "name" => "My Event",
    "start_time" => $nextWeek,
    "location" => "Beirut"
);
$event_id = $facebook->api("/me/events", "POST", $event_param);
Back to top
 

Using the JS-SDK

FB.api('/me/events','post',{name:"JS-SDK Event",start_time:1272718027,location:"Beirut"},function(resp) {
	alert(resp.id);
});
Back to top
 

Important Notes

  • You need the create_event permission
  • Instead of the privacy field use privacy_type; it seems that Facebook is still using the same back-end for publishing the events as the old REST method events.create.
    But don’t try to use this field with other calls or the event FQL table, instead use privacy
  • Always remember to remove the “timezone” from your start_time and end_time fields to get the expected dates (as recommended by Facebook)
  • While it is not documented, specifying the picture field will create your event with a picture!
  • The “Basic approach” will redirect you to Facebook servers obviously. You may need to post these Data using Ajax or to another end point on your server where you are handling the Graph API call there instead.
    UPDATE: We have written a tutorial on how to handle this case: How To: Create Facebook Events Using Graph API – Advanced
Back to top
  • Wck930

    why it re-direct to the link in $my_url instead of the form supposing to be shown?

    • http://www.masteringapi.com Ibrahim Faour

      $my_url IS the page that has the form, so basically it’s the same page.

  • http://artmsilva.com Art M. Silva

    is there a way to display events from a facebook page on a band’s website? i would love to see a tutorial on that.

    • http://www.masteringapi.com Ibrahim Faour

      Sure, added to the posts queue! ;-)

      • Gary

        Been looking a LONG time for an answer to displaying events from a facebook “band page” to a web page on band’s website. If you could do that, you would be my hero.

  • http://twitter.com/jeevandongre Jeevan Dongre

    How do I do the same thing using javascript api and sdk’s….

  • Jer

    what needs changed to make this work for Fan Pages?

    • http://zilverdistel.myopenid.com/ zilverdistel

      I’m also very interested in this feature.

  • http://twitter.com/jeevandongre Jeevan Dongre

    How do I create the same application using javascript. I am trying to do the same application using javascript, but I am still fighting a lot if you can present a brief demo on javascript that would be very helpful for us.

    • http://www.masteringapi.com Ibrahim Faour

      I’ve updated the article to cover PHP-SDK and JS-SDK.

      • Jeevan Dongre

        thanks Ibrahim but when I used the JS-SDK I am getting an getting ‘undefined’ alert from facebook. Can you help to solve this. Thanks for updating the blog with JS_SDK

        • http://www.masteringapi.com Ibrahim Faour

          The error is not clear, but please note that the JS code above is just the calling part. You still need to include the JS-library and initialize the SDK (for more about this check the documentation)

  • Nagaraja

    Hi Ibrahim,
    After successfull creation of event , event-id page is displayed.. instead i want to redirect to my site..
    Is it possible, if yes please say what are line i need to add.

    Thanks in advance

    • http://www.masteringapi.com Ibrahim Faour

      A new tutorial was published for this purpose, please refer to the new update in the Important notes section!

  • Pingback: How To: Create Facebook Events Using Graph API – Advanced | API? Yes Master!

  • Manoj

    Great buddy

  • Nidhasharma

    Hi,

    is there a way to add picture to an event using javascript sdk?

  • Moodsoup

    Is there a way to change the picture upload to an online picture? Been struggling with this for hours. HELP! 

  • http://www.facebook.com/profile.php?id=734271836 Stuart Barker

    I second the request for a way of getting a bands events out of the api.  I hoping there is a way of getting public events data as json without an access token.  Would be cool to cool to not ask my users to let access their info just so they can see some public events.

    • http://www.masteringapi.com Ibrahim Faour

      Well, I guess you can query an upcoming “public” events with your access token (or maybe an app access token?).

  • Barfly777

    amazing works 1st out of the box – thank you very much ;-)

    but … is it possible now to invite all your friends/contacts using the event key?

  • http://www.facebook.com/people/Beta-Tester/100002484004360 Beta Tester

    What if I wanted to erase an event my application created for the user. Is this possible?

  • Volvo Web

    How to delete a note or an event?

  • http://www.facebook.com/aleem.rz Aleem Razzaq

    How can i change location name and its geo coordinate(latitude and longitude) of event? I am fetching events from FB now i want to edit event and i just do this task to edit location and their geo coordinate as well, Please help me on that, em stuck over here since last week. Thnaks

  • Webmaster www.lingua2.eu

     Hi,
    I’m not an expert on facebook api and I have changed these parameters

    $app_id = “xxxxxxxxxxxxxxxxxxxx”;  //my app
    $app_secret = “xxxxxxxxxxxxxxxxxxxxxxxxxxxxx”;  //my secret app
    $my_url = $_SERVER['PHP_SELF'];

    But I don’t know where the following line is retriving the code from:

    $code = $_REQUEST["code"]; //it stays empty

    Should I do anything before executing this script?

  • http://www.facebook.com/erick.oliveira.796 Erick Oliveira

    good night, did not work, and not generate any error could someone help me?

    Thank you.
    Erick

  • http://www.facebook.com/erick.oliveira.796 Erick Oliveira

    good night, did not work, and not generate any error could someone help me?

    Thank you.
    Erick

  • Tharaka

    hello
    i am trying to do to do the same thing with android, can u help me please?,, Really in need of help!!!
    Thank you in advance~!