Code Samples

Using the Code Samples

We're creating some code samples to help get you started using the eMusic API. As time goes on we'll be adding more examples in different languages.


EXAMPLE 1: ALBUM SEARCH WITH JQUERY AJAX AND JSON

This basic example searches for a particular album name and uses the result to display the artist name, album name, and an album image linked to the relevant eMusic page. It uses only front-end technologies to access the API.
Note that due to browser security restrictions, it is not possible access a remote server through an Ajax call unless the JSONP response format is used, as we have here. If you want to receive XML directly in the browser through an API, you'll need to have a server-side script or application handle the API call.

You'll need to grab the jQuery library as well as the eMusic branding image, to reference them locally. Be sure to add your API key in as well.

<html>
<head>
<title>eMusic API example</title>
<style type="text/css">
body {
    background: #ccc;
    margin:20;
    font: 16px "Helvetica Neue", Helvetica, "Arial Unicode MS", Arial, sans-serif;
}
</style>
<script type="text/javascript" src="jquery-1.4.1.min.js"></script>
<script type="text/javascript">

$(document).ready(
    function() {
        
        $.ajax( {
    
            url: "http://api.emusic.com/album/search",
            data: {
                
                perPage: 1,
                format: "JSONP",
                term: "electric+version",
                apiKey: "YOUR_API_KEY_HERE"
            },
            dataType: "jsonp",
            success: function(data) {
                   
                   var album = data.albums[0];
                   
                   $('#albumImage').attr("src", album.image);
                   $('#artistName').html(album.artist.name);
                   $('#albumName').html(album.name);
                   $('a#imageLink').attr("href", album.url);                                      
            },
    
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest + ":" + textStatus + ":" + errorThrown);
            }
        });    
    }
);

</script>
</head>   

<body>
<div id="albumArt"><a id="imageLink"><img id="albumImage" border="0"></a></div>
<div id="artistName"></div>
<div id="albumName"></div>
<div style="margin-top:20px" id="powered"><img id="poweredImage" src="poweredbyemusic_100x38_b.png"></div>
</body>
</html>

YOUR SAMPLES HERE

We'd love to add an example if you have one of your own. Please contact us if you'd like to have your code considered for this page.