64x64 icon dark hosting
Choose a hosting plan here and get a free year's subscription to Tuts+ (worth $180).
Advertisement

How to Use the Latest Updates to the Marketplace API

by
Gift

Start a hosting plan from $3.92/mo and get a free year on Tuts+ (normally $180)

The Envato Marketplaces are exploding, with more than 600k members and over 65k files!

A great addition to the marketplaces is the API, which is becoming more popular everyday, thanks to the latest updates! Our awesome marketplace devs are constantly listening to community feedback, and have added some sweet features to the edge release!

Be sure to check out some of the applications available around the web -- all powered by the Envato API.

Today, we'll take a look at the latest API features! Join me after the jump!


What is an API?

An Application Programming Interface (API) is a particular set of rules and specifications that a software program can follow to access and make use of the services and resources provided by another particular software program. It serves as an interface between different software programs and facilitates their interaction, similar to the way the user interface facilitates interaction between humans and computers.

Web API

Web APIs allow for the combination of multiple services into new applications, known as mashups.

When used in the context of web development, an API is typically a defined set of Hypertext Transfer Protocol (HTTP) request messages, along with a definition of the structure of response messages, which is usually in an Extensible Markup Language (XML) or JavaScript Object Notation (JSON) format.

While "Web API" is virtually a synonym for web service, the recent trend has been moving away from Simple Object Access Protocol (SOAP) based services, toward more direct Representational State Transfer (REST) style communications. Web APIs allow for the combination of multiple services into new applications known as mashups.


Are APIs Used Much?

Most developers will know the answer to this question, but for the others, the answer is a resounding YES.

API's are found everywhere; they're partcicularly popular in the social networking sector! Most large networks, including Twitter and Facebook, provide APIs to interact with their service.


New API Features

In this tutorial, we are only going to take a look at the new API features. If you want to learn more about the existing API features, be sure to check out Drew's tutorial on the topic.

Public

  • item - Details for a single item, specified by the given ID.
  • item-prices - Return available licences and prices for the given item ID.
  • user-items-by-site - Show the number of items an author has for sale on each site. Requires a username, e.g. collis.

Private

  • verify-purchase - Details of a purchase. Requires a purchase code, e.g. verify-purchase:550e8400-e29b-41d4-a716-446655440000.
  • download-purchase - URL to download purchase. Requires a purchase code, e.g. download-purchase:550e8400-e29b-41d4-a716-446655440000

Requirements

Tutorial image

In order to get started with the API, you will need an Envato Marketplace account and an API key.

You can retrieve your API key by visiting your account settings page.

Tutorial image

We'll be using a PHP function, called json_decode; this allows us to retrieve the API data and convert it to an array. This function is included in PHP 5 >= 5.2.0.

Got everything? Let's get started!


Step 1 - Building our Class

Let's begin by building a simple PHP class, which will allow us to retrieve data from the API.

Create a new project folder and create another directory called classes, to keep everything organized. Inside this folder, add a new PHP file called envato.api.class.php

First, we'll construct the basic skeleton of our class:


Step 2 - Global Variables

We need a couple of variables inside our class to make it flexible. To figure out which variables we need, we have to take a look at the API url formatting.

  • Public - http://marketplace.envato.com/api/edge/set.json
  • Private - http://marketplace.envato.com/api/edge/username/api-key/set.json

As you can see, we need a total of four variables:

  • API Url
  • Set
  • Username
  • API Key

Let's create these private variables like so:


Step 3 - Setter & Getter Methods

I like to use setter and getter methods when building classes in PHP. So what exactly are these methods? I've made a small example below:

The code above will not work and should return the following error:

Because the variable is set to private, we cannot access or change it. If we need to change the API url without editing the class file, we could do something along the lines of:

Now switch back to the PHP class and add the function, set_api_url.

Since this function is within our class, we can access and change the private api_url variable. Now in this tutorial, we don't really need getters, but let's still create one to provide you with a better understanding of the concept.

As you may have inferred at this point, we don't need a get function for the API key for example. This makes our class more secure.


Step 4 - Completing the Rest of the Setters

We already finished a setter, so there are three remaining and all of them have the same structure.

Set Username

Set API Key

Set API Set

We are now able to set all variables like so:


Step 4 - Request Data

Let's build a function that will request the data from the Envato Marketplace API. We begin by creating a new public function called request.

We can use the same technique used in the previous tutorial. To request the data from the API, we can use cURL. Let's start of by building the API URL -- if the username and API key are set, we require a different url.

We send a request to the API using cURL like so:

We now have a variable, called $ch_data which contains a JSON formatted string. Before we can do something with this, we need to decode it into a array.

The function in its entirety looks like so:


Step 5 - Testing the Function

Let's try our class and request some data from the API:

Tutorial image

Step 6 - Testing out the New Features of the Public API

Now that our class is working, it is time to build some examples with the new API features.

Item

To request information about a marketplace item, we must supply the API with an item ID. You can find the item ID within the URL of the marketplace item.

Tutorial image

Now that we have our ID, let's request the item information:

The following data should be returned:

Now let's build a simple page that will display the information above in a way that other users will understand it. First, we need to assign the API data to a variable:

We can now display data from the API in the following format: $data['item']['key'].

The HTML after the PHP is parsed:

Tutorial image

item-prices

Instead of creating a new API request to find out more about the item price and license, we can simply add it to the current API set.

The API's response is below:

Now we can use this returned information in our item page.

Tutorial image

user-items-by-site

To finish it off, we are going to append the number of items the user has. We can simply add it to the API set.

And the response:

Let's add this information to our page.

Tutorial image

Step 7 - Testing the New Features of the Private API

In order to use the private API features, you will need a marketplace account and a purchase code from one of your customers. We first need to define this information.

verify-purchase

This will automatically change the API URL from public to the private set. To make things a bit more dynamic, we can create a little form where the user can enter some information to verify his purchase.

Note: When building your own website, don't use inline styles like this!

This gives us a basic form like so:

Tutorial image

Now let's get to the PHP part.

First, we need to check whether the form has been submitted with the requisite fields filled. If you are using this on a live website, remember to add more validation to make things more secure.

And now the API request.

A valid API response will look like so:

If the request is invalid, the API will return an empty array. If we get a response from the API, it means that the purchase code is correct. In the interest of making it more secure, we'll match it to the entered item id and username.

Tutorial image

download-purchase

The download-purchase set allows you to download your own purchases without visiting the marketplace.

Note: The download purchase only works when you are using your own API key and a purchase code of a product that you've purchased.

Now that we have our checkbox, let's make some changes to the verification process.

We'll begin by creating a new variable.

We also need to make some changes to the remaining code:

With those changes in place, the response should look like so:


Conclusion

I hope you've learned a bit about Envato's marketplace API. If so, then put this knowledge to use and build some sweet applications! Thank you so much for reading, and let us know if you have any questions or concerns.

Advertisement