EtsyV2Net & EtsyV2 Cache - Version 2 Etsy API Library
Your First Etsy Search Page - findAllListingActive Keyword Search
The aim of this tutorial is to demonstrate how easy it is to build a search using the EtsyV2Net with only a handful of server-side lines of code. This example is a replication of Etsy's JQuery Demo.
Tutorial Source Code
The source code for this tutorial is included with the EtstV2NET download and also includes three (3) other examples: View Etsy's Current Front Page, Find Treasury by Username, Find Treasury by Keyword(s).
Live Demonstration
Before we get started you can see a live demonstration of findAllListingActive Keyword Search.
Step 1
We need to create a new web project and reference EtsyV2Net library.
Step 2
Ok. Now we're ready to build the search page. When you create a new website project, Visual Studio adds a Default.aspx page. For the purpose of this tutorial we'll work with this page.
The page will have two parts to it:
- Search
- Resultset
First thing, we need to add the search facility. So we need to add a label, textbox, and a button to the client .aspx page code:
<fieldset>
<asp:Label ID="label1" runat="server" Text="Listing Keyword Search: " AssociatedControlID="txtSearch"></asp:Label>
<asp:TextBox ID="txtSearch" CssClass="name" runat="server" EnableTheming="False" EnableViewState="False"></asp:TextBox>
<asp:Button ID="butSearch" CssClass="button" runat="server" Text="Search!" EnableTheming="False" EnableViewState="False"/>
</fieldset>
Next we're going to add a placeholder for the Search Results:
<div id="etsy-images">
<asp:Literal ID="litResults" runat="server" EnableViewState="False"></asp:Literal>
</div>
Step 3
Next we need to go to the code-behind page Defauly.aspx.vb and add a reference to EtsyV2Net:
Const CONSUMER_KEY As String = "" ' API DEVELOPER KEY
Const CONSUMER_KEY_SECRET As String = "" ' AND SECRET FROM ETSY
Protected MyEtsy As New EtsyV2Net.exEtsyNet(CONSUMER_KEY, CONSUMER_KEY_SECRET, False) ' TRUE IF USING SANDBOX MODE
Step 4
Now add code to handle the postback and call Etsy:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
txtSearch.Focus()
If Page.IsPostBack AndAlso txtSearch.Text.Length Then
GetListings()
End If
End Sub
Step 5
Code to call Etsy and format the data for display:
Sub GetListings()
Dim MyListings As EtsyV2Net.Listings(Of EtsyV2Net.Listing) = Nothing
Dim myFields As String = "listing_id,title,url"
Dim myIncludes As String = "Images(listing_image_id,url_75x75):1:0,Shop(shop_id,shop_name)"
Dim myLimit As Integer = 24
Try
MyListings = MyEtsy.findAllListingActive(keywords:=txtSearch.Text, Fields:=myFields, Includes:=myIncludes, limit:=myLimit)
Catch ex As Exception
'!! handle error here
Return
End Try
If MyListings IsNot Nothing AndAlso MyListings.Count Then
Dim sb As New StringBuilder
Dim listingTemplate As String = "<a href=""{0}"" title=""{2} by {3}""><img src=""{1}"" alt=""{2} by {3}"" width=""75"" height=""75""></a>"
For i As Integer = 0 To MyListings.Count - 1
With MyListings(i)
If .Shop IsNot Nothing Then
Dim imageUrl As String = "#"
If .Images IsNot Nothing AndAlso .Images.Count Then
imageUrl = .Images(0).url_75x75
End If
sb.AppendFormat(listingTemplate, .url, imageUrl, .title, .Shop.shop_name)
End If
End With
If (i + 1) Mod 6 = 0 Then sb.Append("<br />")
Next
sb.AppendFormat("<br />{0:#,##0} results matched.<br />{1} records returned in {2:0.0000} seconds & {3:#,##0} bytes received.", _
MyListings.ResultCount, _
If(MyListings.ResultCount < myLimit, MyListings.ResultCount, myLimit), _
MyEtsy.RetrievalTimeTaken, _
MyEtsy.BytesReceived)
litResults.Text = sb.ToString
Else
litResults.Text = "0 results found."
End If
End Sub
When calling Etsy with V2 API, you can specify exactly what data fields and associations you need. Here we ask for listing_id,title,url Listing fields and Images(listing_image_id,url_75x75):1:0,Shop(shop_id,shop_name) associations. You will notice that Images and Shop accociations have fields in brackets and the Images is specifying a limit and offset of the first record only which represents the default or main image for every Listing. More information on fields and associations can be found on Etsy's Developer Pages.
That's it!
And that's it! Fire up the code and try searching 'omg' (or your own keyword(s)).
We hope that this tutorial's been useful. We'd love to hear of your experiences with the Libraries provided and if you develop an application or website, please let us know by leaving us feedback / suggestions / comments and we hope that you'll give the Libraries credit.
Live Demonstration
Here's the live demonstration link for findAllListingActive Keyword Search.
Happy Coding!
EtsyV2Net Developer Links: Third-Party Applications, Overview, How-To Tutorials, Download, Revision History.
ASP.NET Samples: Listing Keyword Search, Treasury Keyword Search, User Treasury Search, Front Page Display, OAuth Authentication.
Silverlight Samples: Listing Keyword Search, OAuth Authentication.




















































































