Frequently asked questions about the Web API
We have prepared a set of frequently asked questions to demystify possible doubts about the Web API. The Web API is a RESTful interface that allows the ERP to be integrated with other applications and services. It exposes ERP resources through structured endpoints, making operations such as creating, reading, updating and deleting data easier. The Web API architecture includes an integration layer responsible for the connection to the ERP, which manages authentication requests by storing them in a cache based on the token generated. The Web API exposes the public methods of the ERP engine and invokes this API directly, without adding additional business rules. There are, however, some specific Web API methods whose business logic is mirrored in the Web API Controller. For example, the CreateSalesDocument method of the sales controller. Authentication in the Web API uses the OAuth 2.0 standard. Requests must be preceded by a call to the endpoint/token, providing credentials such as username, password, company code, ERP instance, product line and a sessionkey. The response includes an access_token which must be used in the headers of subsequent requests. The sessionkey is a required identifier used to authenticate the ERP Web API. When no sessionKey is provided in the token request, the Web API generates an implicit sessionKey that is associated with the token. This identifier represents the integration context of the client application with the ERP and allows the system to associate requests to a specific session, without the need to maintain user sessions on the server layer (IIS). This sessionKey is the key to being able to make several requests simultaneously, even with different tokens, without the risk of different threads accessing information that concerns another integration context. Main features Best practices The endpoints follow this structure: http://localhost:2018/WebApi/{modulo}/{entidade}/{servico}/ In version 2 of the Web API, endpoints include the v2 prefix, i.e: http://localhost:2018/WebApi/v2/{modulo}/{entidade}/{servico}/ Caption: Swagger is a tool for documenting and invoking Web API methods. As of version 2 of Web API v10, Swagger becomes the presentation page for the Web API, allowing users to explore the available modules, view method details and test calls directly through the interface. Version 2 of the Web API introduces the following improvements over the previous version: In version 2, all the answers follow the structure: { "Version": "string", "StatusCode": 0, "ErrorMessage": "string", "Results": {} } This standardization makes it easier for customers to process responses, regardless of the method used. When the token expires, the Web API returns error 401 - Unauthorized. In this case, a new request must be made to the endpoint/token, using the same method as the initial request. To reuse the same integration context, it is recommended to include the expired token in the Authorization header of the new request. Yes, there is. The Web API provides a logout endpoint that allows resources associated with a given context of use to be released. This method is invoked without parameters, but requires the token to be identified in the Authorization header of the request. Using this endpoint helps free up memory in the IIS process and remove inactive sessions in the database. Yes. It is possible to make simultaneous calls to the Web API, but with some restrictions that depend on the server configuration and the way the sessionkey is used. By default: Behavior is controlled by option < add key="BlockMultipleRequestsPerContext" value="true" / > , where: The application of this definition to false should only be used if it is guaranteed that calls to the Web API will not use the BSO (ERP engine) or PSO (Platform). For example: invoking extensibility endpoints that have their own data access layer. Best practices When the access_token expires, the associated integration context is not automatically freed. This behavior allows that, even when a token expires, a new token request can be made (identifying in the header the token that has since expired) and, therefore, the integration context reused. By default, the integration context (defined by sessionkey, company, instance and product line) remains active even after the token expires. This means that resources remain allocated in the application (memory, user sessions and connection to the ERP, including connections to the database) and can affect the server's performance if they are not manually freed. To free up resources properly, it is necessary to: POST /WebApi/Base/Logout Authorization: Bearer {access_token} Note: failure to free the context can leave sessions stuck in the ERP database and keep objects in memory in IIS. This is especially relevant in systems with high loads or multiple integrations. Best practices The access_token obtained through OAuth 2.0 authentication in the Web API v10 has an expiration time of 20 minutes by default. This time can be set in the Web.config file in AppSetting TokenExpirationMinutes. If you disable the BlockMultipleRequestsPerContext option in the web.config file, the Web API no longer prevents simultaneous calls with the same token and sessionkey. Disabling this option poses a number of risks: It is recommended to: Yes. It is entirely possible to have more than one Web API v10 instance, both on the same IIS server and on several different servers. On the same IIS: On multiple IIS servers: Best practices Yes. Web API v10 uses NLog as a logging mechanism to record activities, errors and internal events. These logs are essential for diagnosing problems and analyzing API behavior. By default, the logs are saved in the AplWebAPI folder. In the logs, the following are recorded: The verbosity level of the logs and the targets can be adjusted in the web.config file, in the logging section. You can set levels such as: Best practicesFrequently Asked Questions
1. What is the ERP Web API?
2. How is the Web API architecture structured?
3. How does the Web API authentication process work?
4. What is the sessionkey and what is its importance in the Web API?
5. What is the structure of the Web API endpoints?
6. What is Swagger and how is it used in the Web API?
7. What are the main news in version 2 of the Web API?
8. How is the response to requests structured in version 2?
9. How to proceed when the authentication token expires?
10. Is there a method to log out and free up resources?
11. Is it possible to make simultaneous calls to the Web API?
12. What happens to the integration context when the token expires?
13. What happens if you disable the BlockMultipleRequestsPerContext option and make simultaneous requests with the same token and sessionkey?
14. Is it possible to have more than one Web API v10 instance on IIS? And on multiple IIS servers?
15. Does Web API v10 generate logs? Where can I consult and configure them?