Master API interaction skills and improve the strength of modern web development! This article will explore excellent API resources and how the APILayer platform can simplify API access.
This article is sponsored by APILayer. Thanks to our partners who support SitePoint
fetch
function is an easy way to interact with the API, which sends a request to the URL and returns a promise for accessing the returned data (usually in JSON format). API stands for application programming interfaces, which can describe any mechanism that allows two programs to communicate with each other. For example, methods like document.insertNode
are not actually part of JavaScript, but part of the DOM API, allowing programs to interact with the DOM.
The Web API allows code to communicate and interact with websites and online services in a controlled manner. Most of the time this is done by sending a request that returns some data (almost always JSON format). This data can then be used in the code.
Many APIs also allow some application workloads to be offloaded to external services. For example, if you want to translate a document, you can use the API without writing all the translation code.
APILayer is a selected API marketplace. APILayer's development team carefully selects each API to ensure its security and stability and can scale as the website grows. All APIs offer free solutions and can get started without providing any payment information. APILayer currently provides nearly 100 APIs and is still increasing. These APIs cover a variety of categories from finance to food, and also include many web development tools such as image processing and spell checking.
Each API has a different paid level, starting with free, the price will increase according to the number of requests. Some paid options may also provide more diverse data requests. APILayer simplifies the API registration process and provides real-time demonstration capabilities, allowing testing of APIs directly in the browser, and provides some sample code to help get started. It also provides complete documentation and comments for each API.
The easiest way to interact with the API through JavaScript is to use fetch
. It requests data asynchronously from the URL and returns a promise that allows access to the returned data.
Let's take a look at a simple example:
fetch("https://geek-jokes.sameerkumar.website/api?format=json") .then(response => response.json()) .then(result => console.log(result.joke)) .catch(error => console.log('error', error.message))
This code will request data from the Geek Jokes API, which returns a random joke (JSON format). We pass the API URL to the fetch
function. This will return a promise, we can link the then
method to it - first, use the json()
method to convert the returned JSON into a JavaScript object, and then simply log the joke
attribute of that object to the console. We also added a catch
block at the end to log any errors.
Fetch
Request accepts a second parameter, where any option of the request, including the header. Here is a fairly standard set of options:
const headers = new Headers() const requestOptions = { method: 'GET', redirect: 'follow', headers }
These can then be added to the request:
fetch("https://geek-jokes.sameerkumar.website/api?format=json", requestOptions) .then(response => response.json()) .then(result => console.log(result.joke)) .catch(error => console.log('error', error.message))
Most services require a key to access the API. This can be included as part of the header or in the URL.
Be sure to keep the API key properly. Typically, they should be stored on the server and encrypted. This is to prevent them from being visible in the source code and being used by others. This is often even more a problem when you pay for your subscription, as companies don't want to provide access to their code for free.
Let's take a look at the six best APIs that developers can access on APILayer. Note that for all the examples below, you have to change the access key (API key) to match your own key.
The Spotify API is one of the largest (and coolest) APIs on APILayer. It allows access to a large amount of music and podcast data on Spotify.
Use this API, you can get data such as albums, album tracks, artists, artist albums, playlists, tracks and track lyrics. This means you can search for artists and receive information about all of their published works/albums, including release dates and recognized writers and musicians.
The World News API allows access to data from thousands of news sources around the world. The results come from various news outlets, which means you can get the same news coverage from various angles.
The API also returns a large amount of data, including the title, attribution, author and summary of each news story, as well as any available images. The API allows you to search for news by date and location, which means you can get the latest news as well as old news in specific regions. You can also specify the language and country of search. There is also a concise AI feature that classifies news stories as “positive” or “negative”, allowing you to further organize the returned news.
Weatherstack API allows access to current and historical weather data at any given location (all over the world). The current feature allows you to access real-time weather data such as temperature, wind direction, air pressure and visibility.
There is a lot of data to access here, especially in the Historical Weather feature, which dates back to July 2008, which means you can get weather data for any date at any location after July 2008. The location and date need to be specified as parameters. The History feature also has an optional "hourly" parameter that can be set to 1 or 0, depending on whether you want the weather data to be divided by hour. If you choose to divide data by hour, you can also set an optional parameter "interval" to 1, 3 (default), 6, 12, or 24, depending on how you want the data to be divided.
Both the history and the current function have optional "units" parameters where you can set the unit of data to "m" (metric), "f" (Fahrenheit), or "s" (science).
Number Verification API allows you to find phone numbers from over 200 countries and verify their authenticity. The API will also return detailed information about the country, country code, mobile operator, and line type (mobile or landline). This will allow you to verify phone numbers provided by the web application user in real time.
Zenserp API is a SERP (Search Engine Results Page) API that allows you to crawl search engine results pages in a clearer object format. Its endpoints include Google, Image Reverse Search, YouTube, Bing, and DuckDuckGo. Google endpoints also include sub-endpoints for images, maps, videos, news, and shopping. The results also include ranking information, which will help you identify popular pages and trends.
Zenscrape API is a web crawling API that, for its basic usage, will return HTML content for any given website. To do this, you only need to provide one parameter - the URL of the target website - and the API key. It will also generate any dynamic HTML on the page from any JavaScript or front-end framework such as React or Vue. This means that the returned HTML will be the same as what the user sees.
These six examples are just the tip of the iceberg of the features provided by APILayer. There are more APIs available, so make sure you fully explore the site and try some of them. For example, we highly recommend the Bad Words API and we all had a great time!
When selecting an API, consider the functionality, ease of use, document quality, and community support of the API. The API should provide the specific functionality required by the project. It should also be easy to use, with simple setup procedures and intuitive commands. Comprehensive and clear documentation is essential to effectively understand how to use the API. Finally, an active community can provide valuable support and resources, especially when it comes to problem solving.
You can determine the reliability of your API by checking the uptime history, response time, and rate limits of your API. Many API providers provide status pages to display this information. Also, consider the reputation of the provider and the experience of other developers who have used the API.
Yes, there are many free APIs available for developers. These include APIs such as weather data, news, cryptocurrency prices, and more. However, remember that free APIs usually have usage restrictions and may provide less functionality than paid APIs.
Some popular APIs in web development include the Google Maps API, the Twitter API, and the Facebook Graph API. These APIs allow developers to integrate features such as map display, social media sharing, and user authentication into their websites.
To effectively learn to use new APIs, please first read the API documentation carefully. This should give you a good idea of what the API does and how it works. You can also find tutorials or examples online, or if you have any difficulties, you can ask for help in the developer community.
REST and SOAP are two different ways to build APIs. REST APIs are usually simpler and more flexible, so they are a popular choice for web development. On the other hand, the SOAP API provides more powerful features, often used in enterprise environments.
Yes, you can build your own API. This can be a complex task that requires a good understanding of the web protocol and data format. However, building your own API gives you complete control over its capabilities and can be a valuable learning experience.
When using API, be sure to consider security issues such as data privacy, authentication and authorization. Ensure that any sensitive data transmitted through the API is encrypted and used secure methods for user authentication and authorization.
If you have API issues, first check the API's documentation and status page to see if there are any known issues. You can also use tools like Postman to test the API and identify any issues. If you are still having problems, consider contacting the API's support team or seeking help in the developer community.
Some emerging trends in API development include the use of GraphQL for more efficient data retrieval, the rise of real-time APIs for instant data updates, and an increasing emphasis on API security in response to the growing data privacy issues.
The above is the detailed content of Best APIs for Developers. For more information, please follow other related articles on the PHP Chinese website!