Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Background

I have no idea how to use an API.

I know that all APIs are different, but I've been doing research and I don't fully understand the documentation that comes along with them. There's a programming competition at my university in a month and a half that I want to compete in (revolved around APIs) but nobody on my team has ever used one. We're computer science majors, so we have experience programming, but we've just never been exposed to an API. I tried looking at Twitter's documentation, but I'm lost.

Would anyone be able to give me some tips on how to get started? Maybe a very easy API with examples, or explaining essential things about common elements of different APIs? I don't need a full-blown tutorial on Stack Overflow; I just need to be pointed in the right direction.

Update

The programming languages that I'm most fluent in are C (simple text editor usually) and Java (Eclipse). In an attempt to be more specific with my question: I understand that APIs (and yes, external libraries are what I was referring to) are simply sets of functions.

Question

I guess what I'm trying to ask is how I would go about accessing those functions. Do I need to download specific files and include them in my programs, or do they need to be accessed remotely, etc.?

share|improve this question
4  
I think you're fundamentally mis-understanding what an API is. You can't do programming of any kind without an API. – skaffman Feb 12 '11 at 16:17
Details, details, details. API is a veery general term, the only answer to 'How to use an API?' I can think of is 'You just use it'. What kind of API are you talking about? – Cat Plus Plus Feb 12 '11 at 16:19
@skaffman: he's probably talking about external libraries. @GRardB: It's next to impossible to answer your question without knowing which programming language you use, which particular API would you like to use and even which development tools (IDE, build tool, etc.) you use. – Goran Jovic Feb 12 '11 at 16:21
@GRardB: do you have access to some sample code that uses the API for various tasks? The early step of picking up a new API is a little like learning a few phrases of a foreign language - a gibberish which you try to mimic, along with the explanation of what that sentence means. – rwong Feb 12 '11 at 21:40

migrated from stackoverflow.com Feb 12 '11 at 21:13

6 Answers

An API is what allows you to interact with something. The Win32 API allows you to interact with Windows, for example. The Twitter API would allow you to interact with Twitter.

Ever used a library? That's an API. Ever used a standard library function? That's part of an API.

Calling std::swap(a,b) is an API function call, to the C++ standard library. Calling TwitterAuthenticate(id) is an API call, to some Twitter API I made up.

share|improve this answer

An API is just a set of functions you can call, along with documentation about how they behave. When you write a Java program and use a Set, knowing that the set has methods insert and remove, those functions are part of the Set API. When you write a C program and call printf, the arguments it takes and its return value are part of its API.

There's nothing to using one; the API just tells you what tools are available for your program to depend on.

share|improve this answer

It's kind of a broad question, API stands for Application Programming Interface (which I'm sure you know by now) and they can be supported for various languages for whatever reason.

APIs for web-based services like Twitter will likely use HTTP requests, you basically make a HTTP request to the Twitter servers (possibly with an authentication key acquired by Twitter) and then you can execute certain things like make a new post, search for posts or users, follow/unfollow users (sorry, I don't actually use Twitter though but those are the kinds of things web-based services could allow via their API).. thus, basically you'd just create a socket and build HTTP headers and send the information as required by Twitter (or any other site you wish to utilize).

If you're programming something system-based, APIs are functions you can use via pre-made functions either by the system or by included files of some sort.

If you were to ask a question regarding a particular API, I'm sure you would get better answers.

share|improve this answer

It really depends on the API. A lot of popular ones lately are web-based, meaning to use them you basically send a formatted request to a server and parse the response using whatever means you like.

For a C library, you generally just add an #include to your file, and when you compile it, you add a -I option to point to the right include files, and a -l option to link with the library implementation.

For example, to use libxml2 on linux, you add #include <libxml/parser.h> to your file, and compile with:

gcc -I/usr/include/libxml2 -lxml2 myfile.c

If you told us which one you were specifically interested in, we can give you better guidance.

share|improve this answer

An Application Programming Interface (API) is a doorway into the programming model that a company exposes so you can enhance or extend some capability they provide. For example, Facebook wants to help programmers develop applications that work with their platform. So they offer a library that you include with your application to access data in the Facebook system.

In most cases the best platform to use these public APIs is Windows, simply because it has the largest developer audience.

So how to do you get started? You download an SDK (software development kit). SDKs include the libraries, documentation, samples, help files, and so on that the company uses to help you get started. I've only worked on about 18 SDKs, so I'm no expert, but let's take Facebook for an example. Go to developers.facebook.com. Search for "SDK". Download the version for your platform. Look at the samples. Try them. Change them to do something slightly different.

And yes, it's difficult. But let me give you a hint. It's also fun. In an intellectually stimulating way. Sort of like when you first develop a chess strategy.

share|improve this answer

If there's one thing you should learn about to be ready for APIs in general, it's inversion of control.

http://en.wikipedia.org/wiki/Inversion_of_control

It's not needed for every API, of course, but it's very common in larger APIs.

Particularly worth reading...

http://martinfowler.com/articles/injection.html

Dependency injection is far from the only form of inversion of control. For example, in a Win32 GUI application, the GUI is (mostly) in control - your application is expected to react to messages (event driven model) on demand, but otherwise to be a good neighbour (don't hog the system resources etc). Giving control back to the system in a timely way was absolutely essential back in the Win16 days, when there was no pre-emptive multitasking on Windows.

Even an abstract class with a pure virtual method for you to override is a form of inversion of control, assuming that something else expects you to implement that pure virtual method so it can call you. This is very common, e.g. with GUI libraries that require you to inherit from an "application" class.

However, dependency injection is very important in these object-oriented times, and most/all framework-style APIs use it.

share|improve this answer
-1: what in the world does inversion of control have to do with APIs? – John Saunders Feb 12 '11 at 22:21
@John - many APIs use inversion of control, and you need to understand what is going on to be able to use those APIs. I already listed two broad examples (GUIs and web frameworks) in the answer. For a vague, nearly meaningless question, this is the best answer I could come up with. Perhaps you might ask yourself "could I have thought of a better answer" before downvoting - I notice you haven't left an answer yourself. – Steve314 Feb 13 '11 at 15:31
I blew my nose while ago, and that was a better answer. – John Saunders Feb 13 '11 at 15:36
Well, I guess I deserved that for my own snotty comment. – Steve314 Feb 13 '11 at 16:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.