An API (application programming interface) is the heart of any application development. It allows software programs to interact with each other, the communication point for applications. A user request is sent from the app to the web server via the API. Then, the web server replies to that request via the API response.
Also, APIs play a major role in communicating to our mobile apps and in updating them. In this blog, we will see eight simple tips that need to be considered when using an API in our mobile apps for better productivity!
Let’s explore them!
1. Check internet connectivity before API call
Before calling an API from the mobile app to get data from the server or post data to it, we need to confirm internet connectivity. If there is no internet connection, we can’t call an API and we have to notify the end user about the internet connection failure. This will avoid application crashes.
There are two ways to verify the internet connection, one is before each API call and the other is making a global callback in your app to notify if the internet connection status changes.
2. API versioning
Users’ requirements may change over time, and therefore the API logic will also change over time based on their requirements. Even though the API changes, the API clients (mobile app) must keep working.
If it is a non-breaking change, like additional functionality added in the API, there are no issues to deal with. But, if it is a breaking change, we need to handle that in the API client app too.
So, we need to update the mobile apps immediately after the API changes. The App Store and Play Store take some time to complete their review for new app updates. Also, many existing users won’t update to the newer version immediately. In these cases, we should support the existing versions until the users update the app with the latest version. The solution to this case is API versioning.
With the API version endpoints /api/v1/get and /api/v2/get, new changes will be available in the latest API version and the existing app will still be able to access the older API version. After a while, you can confirm the old version API traffic has ceased and stop keeping the old version available.
3. Show loading/progress indicator until API responds
After requesting the API, it may take some time to get a response from the server. During this standby time, we should show the loading progress to the end user to indicate that something is going on in the background and instruct them to wait until it is completed.
In most cases, the loading/progress indicator for the API request will be displayed and hidden after a successful response from the server.
But the loading/progress indicator for a failed response won’t disappear. In this case, we should hide the loading/progress indicator, otherwise, it will appear indefinitely and may ask the user to try again.
We can show different loading pop-ups based on our needs. For normal data requests, it is sufficient to display a loading pop-up with text. But for the API request to download files, we should show the completed percentage in the progress indicator to notify the user.
4. Messaging
We can set a common error message for all API error responses, but this is not recommended. Instead of hardcoding a common message for all API errors, follow the standard error response with a message and status code. With this, we can show error messages based on the current user action. This will help the end-user easily understand the reason the request failed.
Refer to the following code example for a JSON error response.
{ “StatusCode” : 403, “Message”: “You don’t have permission to edit this content” }
5. Pagination/lazy loading
The screen size of a mobile device is smaller than the desktop. If we pull a huge amount of data from the server, we have to wait for the response and can see only a few items on the mobile screen.
To avoid this, you can use pagination and request the required number of items from the server based on your screen size. The API will respond quickly when we request a small number of items. This will reduce the loading time. Then, we can request the next set of data when we scroll or move to the next page.
6. Log out a user remotely
Compared to web apps, mobile apps perform the login process at once. They do not ask the end user to log in every time while opening the app.
For example: In a mobile payment app we will log in first. But the account status verification alone is not sufficient for the login process. User accounts may expire after logging in. For this case, we have to return the proper status code from the API to the client’s request. Based on the API status code, the mobile app either redirects to the payment page or logs the user out of the app. Refer to the following examples:
- Status code 404: Payment required, redirect to the account page and inform to renew or pay.
- Status code 401: Unauthorized, redirect to the login page and ask to log in again.
7. Force to update the application
After making major changes to both the API and the mobile app, we may plan to discontinue support for older versions. However, the user may still use the older versions.
So, we have to remind the user to update the app to the latest version. To do this, send the application version on the request headers. The API will return the appropriate message and status code. Then, the client-side code will prompt the end user for an update request based on the API response.
Refer to the following screenshot.
8. Send network type in API request
If your app supports video streaming, then you may handle different file qualities based on the mobile network type. In this scenario, send the network type in the request header. So based on the network type, the API will respond quickly.
Examples:
- Network type: Edge. Send a low-quality video.
- Network type: LTE. Send a medium-quality video.
- Network type: Wi-Fi. Send a high-quality video.
Conclusion
Thanks for reading! In this blog, we have covered eight simple tips to enhance your productivity while using an API in mobile app development. I have provided these tips based on what I have learned personally while working on mobile apps. You may have other tips, and I hope you will share your opinion and advice in the comments section of this blog post.
Since this blog explores only simple things, I intentionally didn’t mention API security. API security is an important topic, for which we will publish a separate blog later.
Syncfusion has over 1,600 components and frameworks for WinForms, WPF, WinUI, ASP.NET (Web Forms, MVC, Core), UWP, Xamarin, Flutter, JavaScript, Angular, Blazor, Vue, and React. Use them to boost your application development speed.
For existing customers, the new version is available for download from the License and Downloads page. If you are not yet a Syncfusion customer, you can try our 30-day free trial to check out our available features.
Also, you can contact us through our support forum, feedback portal, or support portal. We are always happy to assist you!