Hey! I'm Josh. This is the github repo for my latest project api-js. Api-js is a javascript library built for people who want to access apis without writing complex scripts for days.
I designed api-js to save people time, and allow all developers to access the fantastic json apis out there. I first started building api-js for my own use whilst building a Reddit web application.
api-js allows Javascript Novices (and Javascript Ninjas) who wish to save time writing Api Calls to use a simple function to do everything they need.
Using api-js you can quickly write, in pure javascript, api calls which normally would require most users to use PHP and Python. This has two benifits: 1. It Saves You Time! - You can implement this function in 10 seconds whilst writing a script in PHP and Python could take hours. 2. It helps your server! - api-js does all the work client-side, this means your server does much less work than had you used PHP or Python.
api-js provides you with a set of javascript functions to easily, and quickly use apis in your code.
Note:
- api-js is still in development.
- Not All Planned Functions Have Been Implemented Yet. Please Check Here Lately for New Updates
- Currently Only Supports JSON apis.
Its Easy! Set Up a Normal HTML Document and Add:
<script src = "api.js"></script>
Inside Your Header
To get a basic JSON Api you use this syntax:
getJSON(url, successfunction);
URL - The URL you wish to get the JSON from.
- eg. http://api.reddit.com/r/funny/top Is the URL for the top posts in r/funny
- eg http://api.reddit.com/r/funny/top?limit=10 Is the URL for the top 10 posts in r/funny.
Successfunction - The function to be called when the data is returned.
- eg. getJSON(url, function(data){console.log(data)}) Logs the returned data in the console
- eg getJSON(url, myfunction) Executes a function you declair called myfunction.
What Really Shows How Useful api.js can be is this demo: Using a question on stackoverflow (http://stackoverflow.com/questions/10341135/example-of-using-github-api-from-javascript) asking for help using the github api the complex solution required 8 lines of code, mine required 2 and produces perfectly readable code.
getJSON('https://api.github.com/users/funchal', function myfunction(data) {
console.log(data.name + " has " + data.public_repos + " public repositories!"); });
The code below when executed collects the top posts in /r/aww then executes the function I declaired called success. The success function then sorts out an array of the posts from the json data. The function then goes through the posts one by one writing to an element called container a link to the image and the caption.
<body>
<h1> Demo A. </h1>
<div id="Container"></div>
<script>
getJSON('http://api.reddit.com/r/aww/top', success);
function success(data) {
redditPosts = data.data.children;
for (i = 0; i < redditPosts.length; i++) {
Post = redditPosts[i].data;
document.getElementById("Container").innerHTML += "<a href = '" + Post.url + "'>" + Post.title + "</a><br>";
}
}
</script>
</body>
Note: Previously before using api.js to write out the exact same data required 24 lines. Using api.js it required 8 lines of javascript.
Please Support My Project By Using it!