Twitter: raymondcamden


Address: Lafayette, LA, USA

First draft - JavaScript Library for Behance Integration

10-16-2013 4,841 views Development, Design

Back in April of this year I blogged about how you could create a "widget" version of your Behance portfolio on your web site. This was done using a little library I built and a user's RSS feed. By using the RSS feed we could bypass the somewhat strict limits on the Behance API (150 hits per hour). I had a few people ask about using the real API so I decided to take a stab at it today.

I've begun with the simplest use case imaginable, getting the projects for a single user. First, you begin with your HTML template.

<!doctype html>
<html lang="en">
	<head>
	</head>
	
	<body>
		
		<h1>Regular Site</h1>
		
		<p>
		Some vanilla content.
		</p>

		<h1>My Projects</h1>
		<div id="projects">
		
		</div>
		
		<script src="jquery-2.0.0.min.js"></script>
		<script src="behance_api.js"></script>
		<script src="demo1.js"></script>
	</body>
</html>

Nothing special here. I've got a basic template with an empty div block I'll be using to display my templates. I'm loading jQuery as a dependency, my new library, and demo1.js, which is just the JavaScript file for this page. Let's look at that next.

/* global $,console,document,behanceAPI */
var behancekey = "vNvOiZI0cD9yfx0z4es9Ix6r4L2J7KdI";

$(document).ready(function() {
	
	//Set my key
	behanceAPI.setKey(behancekey);
	
	//Now get my projects
	behanceAPI.getProjects("gwilson", function(p) {

		//Manually draw them out	
		console.dir(p);
		var s = "";
		for(var i=0; i<p.length; i++) {
			var proj = p[i];
			s += "<h2>" + proj.name + "</h2>";
			s += "<p>";
			s += "<a href='" + proj.url + "'><img src='" + proj.covers[404] + "'></a>";
			s += "</p>";
		}
		$("#projects").html(s);
	});

});

In this demo, all I'm doing is setting the API key (see note below) and then running the getProjects call. The method takes two arguments - the user and the function to run after the data has been fetched. I do not have any error handling yet, that will come later. The result is simply an array of projects that you can display however you want. In this case I just create a big ugly string. Typically you would want to use something like Handlebars instead.

The actual library itself is pretty simple:

/* global console,$ */
var behanceAPI = function() {
	var key;
	var baseURL = "http://www.behance.net/v2/";
	
	function setKey(k) {
		key = k;	
	}
	
	function getProjects(user, cb) {
		var url = baseURL + "users/" + user + "/projects?api_key=" + key + "&callback=";
		console.log(url);
		$.get(url, {}, function(res, code) {
			cb(res.projects);
		}, "JSONP");
	}
	
	return {
		setKey: setKey,
		getProjects: getProjects	
	};
	
}();

So, because of the limits Behance puts on API calls, I decided not to put this demo online. (And yes, I realize my own key is up there!) Instead, I've attached a zip of the project to this blog entry. Here is a screenshot of the result:

Not terribly exciting yet, I know. But - I'm thinking that in my next revision, I'm going to allow you to create a simple template, pass it to the library, and have the output handled for your automatically. My thinking is that many of the people who may use this API are creatives who may not have much JavaScript experience.

Download attached file

Related Blog Entries