Using and connecting to the beseller platform API. API Basics. Creating your own API

The first version of Android appeared in October 2008 - just 4 years ago, which is not that long for an operating system. During this time, more than two dozen updates have already been released. The bulk of the updates included new elements software interface ( API), which applications could use for their needs. So that a particular version of Android knows whether it can satisfy the software requests of a particular application, numbering of software interfaces was introduced. The number characterizing the interface version was embedded in the application itself, and the system used it to determine the compatibility of this application with itself. This number was called the "API Level". There are already 17 levels in total, each subsequent one includes all the functions of the previous one and adds new ones.

To write an application, an SDK is installed on the computer along with the programming system ( software development kit) is a development kit, the main part of which is a class library of the corresponding API level. Each API level has its own library, the functions of which are used by the application. If we use a library of the first API level to develop our application, we will not have at our disposal the functions that appeared later. If we take the latest level library, we risk making our application incompatible with older versions of Android if we suddenly use a function that was not previously supported. So how do you select an API level?

In our application, we must install two API levels (there is also a third, but it has actually died out as unnecessary). The first is the minimum API level at which the application will run, and the second is the target API level, that is, the API level at which the application is compiled. When publishing an application, this level should be the maximum available at that time, so that in new versions of Android it does not work in compatibility mode, but uses all the bells and whistles of the new version of the operating system (independent of the author of the program itself). At the time of writing a program, it should be compiled to the minimum possible API level so as not to use new functions.

As a result, we get the following application development methodology:

  1. We study the target audience - what version of Android is installed on their smartphones. If we are writing for everyone, it will be useful to familiarize yourself with the constantly updated graphs of the use of one or another version of the system on mobile devices: http://developer.android.com/intl/ru/about/dashboards/index.html. Today we see that the bulk are already at API level 10, but there are also large chunks sitting at levels 7 and 8. The differences in levels can be seen here (http://developer.android.com/intl/ru/guide /topics/manifest/uses-sdk-element.html) by clicking on the corresponding API number in the table.
  2. If we don’t want to lose about 10% of the audience, we choose the minimum API level 7. Otherwise, we can choose API level 10, which is where almost half of the users are today. Install the SDK corresponding to the selected minimum level.
  3. We set the target level equal to the minimum, write and compile a program for it. We launch and test it on a smartphone emulator with a minimum level API installed.
  4. After writing and debugging the program, we increase the target level by one, compile the program under the SDK of the new target level and test it in a new smartphone emulator with the appropriate level. Next, we continue to increase the target API level, compile and test the program until we reach the highest API level.
  5. That's it, now you can publish the program. When a new version of Android is released with a new API level, our program will be enabled in compatibility mode and will work as before. To make the program work directly, and not in compatibility mode, we take our project, download the new SDK of the latest API level, and compile the application under it. Now the application will launch on the new system in normal mode, and perhaps will look a little different, in the tradition of the new version of the operating system. At the same time, on older versions of the system the application remains as it was.
As a result, after thinking about it, I settled on API level 7. In addition, when using some new functions that appeared in much later APIs (for example, fragments), the development environment itself suggested that I use a special compatibility package, and added it to the project . As a result, I was able to use some new features from the new APIs in the old one. However, this is another topic.

You've probably seen the term "API". Operating system, web browser, and application updates frequently announce new APIs for developers. But what is an API?

Application Programming Interface

The term API is an acronym and it stands for Application Programming Interface.

An API is like a menu in a restaurant. The menu contains a list of dishes that you can order, as well as a description of each dish. When you specify which menu items you want, the restaurant kitchen does the job and provides you with the finished dishes. You don't know exactly how the restaurant prepares this food, and you don't need to.

Likewise, the API provides many operations that developers can use, as well as a description of what they do. The developer does not need to know how, for example, the operating system is created and the Save As dialog box is displayed. They just need to know that it is available for use in the application.

This isn't a perfect metaphor as developers may have to provide their own API data to get results, so perhaps it's more like a fancy restaurant where you can provide some of your own ingredients for the kitchen to work with.

APIs allow developers to save time by leveraging platform implementation to get important work done. This helps reduce the amount of code to develop and also helps create consistency between applications on the same platform. APIs can control access to hardware and software resources.

APIs make life easier for developers

Let's say you want to develop an iPhone application. Apple's iOS operating system provides a large number of APIs, just like any other operating system, to make this easier for you.

For example, if you want to embed a web browser to display one or more web pages, you don't have to program your own web browser from scratch just for your application. You
You can use the WKWebView API to embed a WebKit (Safari) web browser into your application.

If you want to take photos or videos from the iPhone camera, you don't need to write your own camera interface. You can use the Camera API to embed the iPhone camera into your app. If the API didn't exist, app developers would have to create their own camera software and interpret camera hardware inputs. But Apple's operating system developers have done all that hard work, so developers can simply use the camera API to embed the camera and then continue writing their app. And when Apple improves the camera API, all apps that use it will automatically take advantage of that improvement.

This applies to all platforms. For example, do you want to create a dialog box in Windows? There is an API for this. Want to support fingerprint authentication on Android? There's an API for this, so you don't have to test every fingerprint sensor from every Android manufacturer. Developers don't have to reinvent the wheel over and over again.

APIs control access to resources

APIs are also used to control access to hardware devices and software features that the application may not have permission to use. This is why APIs often play a big role in security.

For example, if you have ever visited a website and seen a message in your browser that the website is asking for your exact location, that website is attempting to use the geolocation API in your web browser. Web browsers provide APIs to make it easy for web developers to access your location - they can simply ask "where are you?" and the browser will do the hard work of accessing GPS or nearby Wi-Fi networks to find your physical location. location.

However, browsers also expose this information through APIs because access to it can be controlled. When a website wants to access your exact location, the only way to get it is through the location API. And, when a website tries to use it, you—the user—can allow or deny the request. Hardware resources such as a GPS sensor can only be accessed through an API, so the browser can control access to the hardware and limit what apps can do.

The same principle is used for modern mobile operating systems such as iOS and Android, where mobile applications have permissions that can be enforced by controlling access to APIs. For example, if a developer tries to access the camera through the camera API, you can deny the permission request and the app will not have access to your device's camera.

File systems that use permissions, like Windows, Mac, and Linux, have those rights that are enforced by the file system API. A typical application does not have direct access to a raw physical hard drive. Instead, the application must access the files through the API.

APIs are used for communication between services

APIs are also used for other reasons. For example, if you have ever seen a Google Maps object embedded on a website, that website uses the Google Maps API to embed that map. Google provides APIs like these for web developers, who can then use the APIs to assemble complex objects directly on their website. If no such APIs exist, developers may have to create their own maps and provide their own map data in order to host a small interactive map on a website.

And because it's an API, Google can control access to Google Maps on third-party websites, ensuring that they use it in a consistent manner rather than trying to randomly implement the frame that the Google Maps website shows, for example.

This applies to many different online services. There are APIs for requesting a translation of text from Google Translate or displaying Facebook comments or Twitter tweets on a website.

The OAuth standard also defines a number of APIs that allow you to log into a site through another service, such as using your Facebook, Google, or Twitter accounts to log into a new website without creating a new user account just for that site. APIs are standard contracts that define how developers interact with a service and the type of output that developers should expect to receive.

If you have read this article, you will have a better idea of ​​what an API is. Ultimately, you don't need to know what an API is unless you're a developer. But, if you see that a software platform or service has added new APIs for different hardware or services, it should be easier for developers to use such features.

Let's start with the basics: what is an API? The abbreviation stands for Application Programming Interface, or interface for application programming. The name seems to speak for itself, but it’s better to consider a more detailed explanation.

As already mentioned, an API is, first of all, an interface. An interface that allows developers to use ready-made blocks to build an application. In the case of developing mobile applications, a library for working with a smart home can act as an API - all the nuances are implemented in the library and you only access this API in your code.

In the case of web applications, the API can return data in a format other than standard HTML, making it convenient to use when writing your own applications. Third-party public APIs most often serve data in one of two formats: XML or JSON. In case you decide to make an API for your application, remember that JSON is much more concise and easier to read than XML, and services that provide access to data in XML format are gradually abandoning the latter.

API in web applications with examples

An application - for example, Github - has its own API that other developers can use. How they will use it depends on the capabilities that the API provides and on how well the developers’ imagination works. The GitHub API allows, for example, to obtain information about the user, his avatar, readers, repositories and many other useful and interesting information.

In a similar way, you can send a request in any language, including Ruby. The response to the request will be approximately the following information:

( "login" : "Freika" , "id" : 3738638, "avatar_url" : "https://avatars.githubusercontent.com/u/3738638?v=3", "gravatar_id" : "" , "url" : "https://api.github.com/users/Freika", "html_url" : "https://github.com/Freika" , "followers_url" : "https://api.github.com/users/Freika/followers", "following_url" : "https://api.github.com/users/Freika/following(/other_user)", "gists_url" : "https://api.github.com/users/Freika/gists(/gist_id)", "starred_url" : "https://api.github.com/users/Freika/starred(/owner)(/repo)", "subscriptions_url" : "https://api.github.com/users/Freika/subscriptions", "organizations_url" : "https://api.github.com/users/Freika/orgs", "repos_url" : "https://api.github.com/users/Freika/repos", "events_url" : "https://api.github.com/users/Freika/events(/privacy)", "received_events_url" : "https://api.github.com/users/Freika/received_events", "type" : "User" , "site_admin" : false , "name" : "Evgeniy" , "company" : "" , "blog" : "http://frey.su/" , "location" : " Barnaul" , "email" : "" , "hireable" : true , "bio" : null, "public_repos" : 39, "public_gists" : 13, "followers" : 15, "following" : 21, "created_at" : "2013-03-01T13:48:52Z" , "updated_at" : "2014-12-15T13:55:03Z" )

As can be seen from the block above, the response contains the login, avatar, link to the profile on the site and in the API, user status, number of public repositories and other useful and interesting information.

API alone is not enough

Creating a full-fledged API for your application is only half the battle. How are you supposed to access the API? How will your users access it?

The first thing that comes to mind is the usual series of HTTP requests to obtain the desired information, and this is the wrong answer. The most obvious method in this case is not the most convenient and simple. It would be much more reasonable to create a special library for working with the interface, which will describe all the necessary ways to receive and send information using the API.

Let’s use Github once again to give an example: to work with the API of this excellent service (and its interface provides extensive capabilities), several libraries have been created in various languages, for example the Octokit gem. In the documentation for such libraries (and the gem given as an example), any interested developer will be able to find all the necessary ways to receive information from Github and send it back through the service API.

Thus, if you are creating your own API, consider perhaps creating libraries to work with it in the most common languages. And be prepared that at a certain level of demand for your application, someone else may create their own library to work with your API. This is fine.

useful links

In subsequent articles we will talk about how to correctly create an API, ensure its security and limit access to some information.

Do you have a dog. But she doesn't speak human language. However, she is able to "understand" him through the commands that she was taught during the training process. If you tell a dog who knows the command “slippers!” something like “Rexik, please bring me my slippers with the little bunnies”, he’ll probably listen to the name, but won’t bring the slippers. So, an API is a set of commands with which your dog understands you and does what you need. This is very simplified for a teapot, but the essence is clear, I think.

An API is a language, a regulated way, for one computer program to communicate with another to jointly perform some common task, when one program fulfills the requests of another. Application Programming Interface (API) - Application programming interface.

This is a primitive analogy for dummies that was born offhand.

Imagine 5 foreigners speaking different languages ​​who need to work and live together, say in Russia. Neither of them knows the other’s language, but they need to perform some task as a single team, in roles, for example, feed each other and discuss the taste of food in Russian. To do this, you need to buy groceries, cook food, set the table and discuss dishes while eating. So that they understand each other and can find products in the outside world, we will teach them a basic set of Russian words. Let's imagine what we have:

1. Frenchman

2. Spaniard

4. Englishman

5. Italian

Let's distribute roles between them to perform subtasks as follows:

Buying Food: French and Spanish

Cooking Dishes: Spanish, German and English

Table Setting: English and Italian

Meal and discussion of taste Dishes: ALL

In order for them to be able to complete all these tasks, we will teach everyone a set of those Russian words that will allow them, in cooperation with each other and the external environment, to complete all these tasks. See the picture below.

So here it is. The group of our foreign friends is a group of computer programs who need to communicate with each other and with the external environment.

Language and Words denoting products and basic actions that need to be produced this is an API– the standards by which our foreign friends communicate with each other in Russian in order to complete all assigned subtasks.

API 1: Words for Products and Where to Buy
API 2: Words for Dishes and Cooking Methods
API 3: Words denoting Devices and Actions with them
API 4: Words denoting Taste and Evaluation of Food

It can be more complicated, for example, let API 2 be Turkish, API 3 be Chinese, API 4 be Hindi

Example for dummies:

1. There is an outlet. Behind it lies a huge amount of technology. But to use it, you need to have a plug with a distance between the rods of 3cm and the socket will supply 220V. This is the API interface of a huge electrical production system.

2. Is there an iron? He has his own complex system of work. But in order to work with a socket, it complies with API requirements - you need a plug with a distance of 3 cm and expects 220 volts in response.

That's all. The 2 systems are independent, they are huge and complex. But the API is made to make connecting with each other as simple as possible.

API - application programming interface. This is a certain set of functions, constants, classes and, possibly, other objects for interaction with a certain piece of the program.

I think the clearest way is to describe it with an example. Let's say someone wrote a calculator that you want to use in your program. This calculator needs to be accessed somehow. These ways of interacting with the calculator will be called API. They can be different, and without their description nothing can be done. Maybe you'll have a function to write a number into memory, another to perform an action, and another to get the result. Or maybe you will have one function that will pass two numbers and the operation code between them, and immediately return the answer.

Such descriptions are made for everything. The operating system has an API, this is a set of functions with which a program is created: establish a network connection, draw a window, process a button click. For any server, the API is a set of functions that it performs. The browser accesses the Wikipedia site - it uses the API to return the response to your request.

We've released a new book, Social Media Content Marketing: How to Get Inside Your Followers' Heads and Make Them Fall in Love with Your Brand.

Subscribe

An API is an external interface for programming an application, accessing sites using a specific protocol to obtain information and simplified development of programs associated with internal services.

What does API mean?

The simplest analogy for using an API would be to use a calculator to perform complex calculations. Let's say you have a task, you can understand its essence, build equations and graphs, but you do not know how to perform arithmetic operations with numbers. There is a calculator next to you that can do these operations with ease. You don't know what's going on inside the computer, and you don't need to know. You give information in one form, and receive it in another, necessary for your purposes.

Any API works on this principle. You don’t care how the program gets the answer, what path the request takes inside it, how the calculations are performed. You are sure of only one thing - in response, standardized information will be given about the success of the operation or its error.

The API interface allows you not to waste your time, money and effort on buying a “new bike”. You get a working information port that receives and sends the necessary amounts of data for the purposes of your development.

Pros:

  • Saving on developing your own interface.
  • There is no need to understand the nuances of the issue.
  • APIs are developed by professionals and take into account all the factors of internal processes that you may not be aware of when creating your solution.
  • Allows you to communicate with services that are closed through other protocols.

Minuses:

  • If the target service is updated, the API does not always immediately receive full functionality.
  • You can't catch errors and don't know how the process works in someone else's code.
  • The API does not always give the most optimized result in terms of time, since it is designed to handle general cases, not specific ones.

API examples

API integration is the process of connecting an application to an external data interface. Working with the API begins with studying the documentation and protocols used, and then directly integrating your program into the interface. Let's look at the most popular services that have their own API.

VKAPI

External interface for interaction of the popular social network VKontakte with clients, as well as with browser and server applications. Allows you to manage community messages, group covers, user pages if you have the appropriate access keys.

All requests are made to the address https://api.vk.com/method/

After the slash comes the name of the API method used and the GET parameters of the request are transmitted. The response also comes via HTTPS in JSON format.

TELEGRAM BOT API

One of the most popular APIs. It is used to control bots in the Telegram messenger. After creating a bot via @botfather and obtaining the necessary access keys, you can begin interacting with the backend.

Requests can be made to: https://api.telegram.org/bot0000000:token/

Where instead of bot0000000 the unique identifier of your bot is put, and token expresses the secret key.

Requests are sent via HTTPS connections, the method name is indicated via a slash to the main address. The response comes in JSON format.

OPEN WEATHER MAP API

It is often necessary to obtain weather information without using third-party widgets and paid applications. The OpenWeatherMap service comes to the rescue with an open and free API. After registering and receiving identification data, you can send weather requests from server scripts around the world. In response to the city ID, the resource returns the most detailed information about the current weather and gives a forecast for the near future.

Format: HTTP transmission via api.openweathermap.org/data/2.5/weather?id= indicating the identification number of the desired city. Server response: JSON.

GOOGLE MAPS API

What could be nicer than an interactive world map on a website? Especially if this is not a template insert from Google Maps, but your personal edition of a popular map with personal clusters of markers. The map will interact with other scripts on the site, sending information about clicks and coordinates.

The Google Maps JavaScript API offers similar capabilities. The module is completely scripted and works on the browser side, so we don’t need HTTP requests from PHP and the formation of headers on the server side, as was the case in other APIs.

For example, placing a marker on a map will look like this:

var mark = new google.maps.Marker((
position: myPOS,
map: map,
title:"Hello!"
});

What is the need and benefits of using the API?

There are quite a lot of useful functions.

First aspect

You can establish interactive user interaction with social networks and instant messengers, use the capabilities of third-party computing systems to display exchange rates, weather and other important information.

Using the API, you can instantly connect other resources and software solutions to servers, which would normally take weeks of development. The API simplifies life where a unique implementation is not needed, and reliability and security are a priority.

Second aspect

If you are the owner of complex computing power, a popular service or data storage for public or semi-private access, then a good move would be to raise your own API. What will it give:

  • Large flow of clients.
  • Simplified access to your services for partners.
  • Convenience of statistical analysis of service use.

Third aspect

Almost the same as the second one. But without having to implement an open access API. If you have a portal and want to create a mobile application for it on Android/IOS, then rewriting the system under a single API is the best solution. The entire data structure is systematized. The site and application will operate through single data channels.