GovEvents API — ASP.NET Example and Developer Notes

Response Format

Response format implementations are currently XML and JSON. It is specified by the extension (.xml or .json) or the HTTP Accept header (application/xml or application/json), in order of precedence.

URL Construction

Retrieve upcoming events in XML:
http://www.govevents.com/api/events.xml

Retrieve a specific event (using an event ID returned from the event list):
http://www.govevents.com/api/events/123.xml

The events that can be accessed or returned may be dependant upon your API key.

Note on Event Details When Using XML

If the response format is XML, the <detail> and <vendor_detail> elements are CDATA sections and any HTML is encoded. To decode the HTML entities in ASP.NET, you can use Server.HtmlDecode().

Error Handling

Returns from the API include a "status" element with a value of either "ok" or "error"

The following is an example of an error returned due to invalid authentication:

<response>
	<status>error</status>
	<code>401</code>
	<message>You must be authorized to access this content.</message>
</response>

The error code is returned in the response header as well. An error status of 404 is used in cases where no events are found for the query.

Example Code

Default.aspx.cs

using System;
using System.Collections.Generic;

using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string username = "YOUR_API_KEY";
        string password = "YOUR_API_PASSWORD";
        string url = "http://www.govevents.com/api/events.xml";

        Uri uri = new Uri(url);
        CredentialCache credentials = new CredentialCache();
        credentials.Add(uri, "Digest", new NetworkCredential(username, password));

        WebClient req = new WebClient();
        req.Credentials = credentials;
        string res = req.DownloadString(uri);

        XDocument doc = XDocument.Parse(res);
        var events = from evnt in doc.Descendants("event")
                     select new

                     {
                         Title = evnt.Element("title").Value,
                         Website = evnt.Element("website").Value,
                         Detail = Server.HtmlDecode(evnt.Element("detail").Value)
                     };

        EventsList.DataSource = events;
        EventsList.DataBind();
    }
}

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>GovEvents API</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:Repeater ID="EventsList" runat="server">
            <ItemTemplate>
                <div class="event">
                    <h1><%# Eval("Title") %></h1>
                    <a href="<%# Eval("Website") %>">More Info</a>
                    <div><%# Eval("Detail") %></div>
                </div>
            </ItemTemplate>
        </asp:Repeater>

    </div>
    </form>
</body>

</html>