System for quick creation of websites and online stores

API best practices

Back in the category

  1. Reduce duplicate calls to API requests.
  2. Restrict access tokens to specific IP addresses from which API calls are made.
  3. Regularly delete unused access tokens in the administration.
  4. Use public domain name in URL and correct protocol for API calls. This will prevent unnecessary HTTP redirection.
    Instead of https://your-page.flox.cz/api/graphql, use
    https://www.your-domain.cz/api/graphql
  5. To restrict access to API functions, assign the token to users with limited rights.
  6. For regular updates, ask only for the data you really require. Enter in the query an exact list of parameters that are included in the answer for further processing or use notation using a custom fragment (see example no. 1).
  7. You can combine predefined fragments with a specific enumeration of other parameters.
  8. Divide complex or extensive queries into separate sub-queries and process requests one after the other. For example, if you require a complete structure of e-shop categories and product data at the same time. Process categories and products separately.
  9. Use smaller values for pagination (set the limit parameter to less than 30) when scrolling through lists and complex response structure. This applies to the methods getProductList, getOrderList, getInvoiceList, etc. if you also require linked data at deeper levels.
  10. When performing a mutation, it is advisable to include the currently changed parameters in the answer. At the same time, you will check that the operation was carried out correctly. For example, when changing the status of an order, it is advisable to immediately ask about its status in the reply (see example no. 2)

Examples:

1. Definition of a custom fragment sent along with the query

query {
      getOrderList(lang_code: "SK", params: { order_by: pur_date, sort:DESC, limit:30, cursor:0}) {
           pageInfo{
               ... _PageInfo
          }
          data{
              ... MyOrderData # using your own fragment
          }
    }
} # custom fragment definition for specific order data fragment MyOrderData on Order {
      order_num
pur_date
sum {
formatted
} }

HTTP payload and query

POST /api/graphql HTTP/1.1
Host: yourwebsite.com
BW-API-Key: Token Ijw..................gKsB7
Content-Type: application/json
Cookie: SSID=27s5cke5yr78c441203a01bbur
Content-Length: 500

{"query":"query {\r\n getOrderList(lang_code: \"SK\", params: { order_by: pur_date, sort:DESC, limit:30, cursor:0}) {\r\n pageInfo{\r\n ... _PageInfo\r\n }\r\n data{\r\n ... MyOrderData # using your own fragment\r\n }\r\n }\r\n}\r\n# custom fragment definition for specific order data\r\nfragment MyOrderData on Order {\r\n order_num\r\n pur_date\r\n sum {\r\n formatted\r\n }\r\n}","variables":{}}

2. Order status change. The response contains the changed parameters

mutation {
     changeOrderStatus(order_num: "2302120", status_id: 4){
         #request the changed parameters in mutation response
         order_num
         status {
              id
              name
         }
     }
}

HTTP payload and query

POST /api/graphql HTTP/1.1
Host: yourwebsite.com
BW-API-Key: Token Ijw..................gKsB7
Content-Type: application/json
Cookie: SSID=27s5cke5yr78c441203a01bbur
Content-Length: 281

{"query":"mutation {\r\n changeOrderStatus(order_num: \"2302120\", status_id: 4){\r\n #request the changed parameters in mutation response\r\n order_num\r\n status {\r\n id\r\n name\r\n }\r\n }\r\n}","variables":{}}

3. Writing a query using fragments and separate parameters

query {
       getProduct(product_id: "26049"){
              # fragment _Product contains "attribute_category" with only "id" and "title" params
              ... _Product
              attribute_category {
                    id
                    title
                    # in the query you can override the parameter and spoecify different parameters
                    description
             }
      }
}

HTTP payload and query

POST /api/graphql HTTP/1.1
Host: yourwebsite.com
BW-API-Key: Token Ijw..................gKsB7
Content-Type: application/json
Cookie: SSID=27s5cke5yr78c441203a01bbur
Content-Length: 481

{"query":"query {\r\n getProduct(product_id: \"26049\"){\r\n # fragment _Product contains \"attribute_category\" with only \"id\" and \"title\" params\r\n ... _Product\r\n attribute_category {\r\n id\r\n title\r\n # in the query you can override the parameter and specify different parameters\r\n description\r\n }\r\n }\r\n}","variables":{}}

Would you like to ask more about this topic? Contact us please:

Captcha

i

Free 15-day trial

Design selection:

Your new website with .bizwebs.com will be available immediately.
You can change address to one without .bizwebs.com later, either by buying a new domain or reddirecting an existing one.
By pressing the create button you agree with the Terms & Conditions of Bizwebs.com.

Please select one of premade templates.
You can change design of your new website anytime you want in website administration, where you can find a wealth of template customization.
We can also create a unique design tailored to your wishes.

Login

This website uses cookies. More info
Privacy settings

We use cookies to improve your visit to this website, to analyze its performance and to collect data about its use. We may use third party tools and services to do so and the data collected may be passed on to partners in the EU, the US or other countries. By clicking on "Accept all cookies", you agree to this processing. You can find detailed information or adjust your preferences below.

Privacy declaration

Show details