Meetup.com has a well-documented API that gives you all the information about your events you can think of. Getting the attendees, the pictures taken at your event or the comments is easy, you can even try everything out online without writing a single line of code. That great developer support unfortunately ends rather abruptly when it comes to creating an event using the API. There is no online demo and the documentation itself guides you into the wrong direction.
Prerequisites
You need your API key and a group in which you can create events. Without either one of them it will be impossible to try the code of this post on your own.
JSON does not work
The API for all the GET requests delivers JSON back and that my give you the idea that you can create a JSON message with your event and post it to the server. I tried this but only got this useless error message:
1 2 3 4 5 6 7 8 9 |
{ "errors": [ { "code": "name_error", "field": "name", "message": "A title is required to post your Meetup" } ] } |
Post a form using FormUrlEncodedContent
The documentation for creating events contains a easy to miss heading named Request Parameters. This is exactly what you need to create an event: not JSON, but a plain simple old boring form request. The fields must be named exactly as they are written in the documentation, including the _ and everything must be in lower case.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
var myEvent = new PushEventDto() { Announce = false, Name = "Lessons learned", Description = "The 5 things we learned while...", EventHosts = "222967033,242384965,187606307", GuestLimit = 22, VenueId = "26231286", Time = new DateTime(2020, 4, 28, 18, 00, 0) .ToUniversalTime() .ToDateTimeOffset() .ToUnixTimeMilliseconds(), Duration = 90 * 60 * 1000 }; var formContent = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("name", myEvent.Name), new KeyValuePair<string, string>("description", myEvent.Description), new KeyValuePair<string, string>("publish_status", "draft"), new KeyValuePair<string, string>("venue_id", myEvent.VenueId), new KeyValuePair<string, string>("event_hosts", myEvent.EventHosts), new KeyValuePair<string, string>("time", myEvent.Time.ToString()), new KeyValuePair<string, string>("duration", myEvent.Duration.ToString()), new KeyValuePair<string, string>("guest_limit", myEvent.GuestLimit.ToString()) }); using (var client = new HttpClient()) { var group = "YOUR_GROUP_NAME"; var apiKey = "YOUR_API_KEY"; var url = $"https://api.meetup.com/{group}/events?key={apiKey}&sign=true"; var response = await client.PostAsync(url, formContent); // Get the response var result = await response.Content.ReadAsStringAsync(); var newEvent = JsonConvert.DeserializeObject<CreatedEvent>(result); Console.WriteLine($"new event Id: {newEvent.id}"); } |
A few important parts to point out with the code above:
- The types for the
KeyValuePair
in theFormUrlEncodedContent
must be both strings. Otherwise you may get an error. - When
publish_status
is set todraft
, you can see your newly created event on meetup, but the members of your group will not get an email about a new event. When everything works you can change it topublished
. - The
event_hosts
property can contain multiple hosts. However, it only works when you havenumer,number
without any spaces. - Converting a
System.DateTime
to milliseconds since the epoch needs a few method calls. Do not forget the conversion to UTC or the start time of your event will be off by a few hours. - The
duration
is in milliseconds, what makes not much sense but needs to be considered when you want to change it from the default (3 hours). - The response for your created event is in JSON. You can use the response string to create a class using the Paste JSON as classes feature of Visual Studio and then deserialize it with Newtonsoft.JSON into an instance of that class.
Conclusion
As soon as you drop the idea of using JSON and go back to the old days of forms you can create events with the Meetup API without any problems. It would be great if that little detail would be written directly in the documentation – that would have saved us hours of trying the wrong approach.