Managing In-App Items Using the Galaxy Store Developer API and Python
galaxy store provides a platform where developers can sell their apps to galaxy device users. samsung in-app purchase (iap) is a payment service which allows you to sell your app's in-app items that are distributed through galaxy store. if you want to make any changes to your app's in-app items, then you have to update your app using galaxy store seller portal. however, if you want the changes to be reflected immediately within the content, even when the content is in the for sale state, then the galaxy store developer api is the solution for you. the galaxy store developer api provides programmatic access to the same types of functionality provided by the galaxy store seller portal. the iap publish api is a part of the galaxy store developer api, a set of server-to-server apis that allows you to manage in-app items. this api is used to view, register, modify, and remove samsung iap items. you can find the curl commands for this api in the iap publish api guide. in this blog article, i am going to demonstrate how to implement the iap publish api using python. let's get started! get started in this example, you should already know about the in-app purchase sdk and how to create items in seller portal. to learn how to sell in-app items through galaxy store, please read the in-app purchase programming guide. the iap publish api supports crud operations for in-app items: create, read, update, and delete. the available http methods for these operations are post, get, put, patch, and delete, respectively. later, i explain which http method is used for which iap publish api. since i implement this api in python, i need to install python on my pc. the rest of this blog assumes you have already done this. prerequisites: seller account: create a seller account in galaxy store seller portal using the galaxy store seller portal user guide. service account id: get the service account id from seller portal. you can find the details here. authorization: get the access token from the galaxy store authentication server using the accesstoken api. check out this blog to learn about creating an access token using python. iap sdk integrated app: the iap sdk must be integrated into the app. all binaries registered in the content must support iap. connection with the content of the in-app purchase tab area of seller portal: to create this connection, you must either initially distribute your content using the content publish api or the content must be in the for sale state in seller portal. the following headers are required to call any api, just like any other galaxy store developer api. attribute type description content-type string application/json authorization string bearer <your-access-token> service-account-id header your service account id the iap publish api provides a total of six apis to manage in-app items. after fulfilling the above requirements, i am ready to implement these apis in python. view item list i can view the list of item information within a scope. while sending this request, i have to specify the number of pages of content to return and the number of items to be returned for a page. since this is a rest api, i have to make an http request. to make an http request to a specified url, i need the requests library. so, first i have to import the library in our script. i also import the json library to see the response in json format. # importing the requests library import requests # importing the json library import json as mentioned earlier, i need to send some data as the header in the request. first i set these values before creating the header. then i create a dictionary using the syntax {key: value}. here, the key is the attribute name and the value is the header content. access_token = "<access_token>" authorization = "bearer " + access_token service_account_id = "<your-service-account-id>" # header to be sent to the api headers= { 'authorization': authorization, 'content-type': 'application/json', 'service-account-id': service_account_id } the get method is used to read or retrieve data from the given server using a given url. it doesn't change any data. therefore, i use an http get request for this api. in the get request, the package name of an app, the number of pages, and the number of items are required. as an example, i set the number of pages as 1 and the number of items as 20. packagename = "<package-name-of-your-app>" page = '1' size = '20' now, i define the url and send the get request. # defining the api endpoint item_list = 'https://devapi.samsungapps.com/iap/v6/applications/' + packagename + '/items?page=' + page + '&size=' + size payload={} try: response = requests.get(item_list, headers=headers,data=payload) print(response.status_code) pretty_json = json.loads(response.text) print (json.dumps(pretty_json, indent=2)) except exception as e: print(str(e)) figure 1: response of the view item list api view individual item i use this api to view the detailed information of one item. the item id is a required parameter for this api, being a unique identifier of an in-app item registered in seller portal. the python script for this api is similar to the view item list api; i just need to change the request url. itemname = '<item-id-of-specific-package-id>' # defining the api endpoint individual_item = 'https://devapi.samsungapps.com/iap/v6/applications/' + packagename + '/items/' + itemname try: response = requests.get(individual_item, headers=headers,data=payload) print(response.status_code) pretty_json = json.loads(response.text) print (json.dumps(pretty_json, indent=2)) except exception as e: print(str(e)) figure 2: response of the view individual item api create item this api allows you to create a new item, which can be either consumable or non-consumable. to learn more about the item types, visit the iap programming guide. to register an item, you are required to set nine parameters as the data in an http post request. the post method is used for creating a new resource. first, let's go through an example where i create a new item called 'one_gallon_gas'. i set a title of this item and a description which explains the purpose of this item. to be able to purchase the item multiple times, i set its type as consumable. i set the 'phonebillstatus' attribute as 'true' so that the user can pay by an automatic payment on their phone bill. i set the item status as 'published' to list it for sale in galaxy store. finally, i set the usd price, local price, country id, and currency values. next, i create a dictionary to hold the data which is sent to the api. i need to send serialized data in a form of a string and thus json.dumps is necessary to perform the serialization. # data to be sent payload = json.dumps({ "id": "one_gallon_gas", "description": "fuel for driving game", "status": "published", "type": "consumable", "usdprice": 0.99, "title": " 1 gallon gas", "prices": [ { "countryid": "nga", "currency": "ngn", "localprice": "400.000" }, { "countryid": "per", "currency": "pen", "localprice": "3.900" }, { "countryid": "swe", "currency": "sek", "localprice": "11.000" } ] }) finally, i define the url and send the post request. new_item = "https://devapi.samsungapps.com/iap/v6/applications/" + packagename + "/items" try: response = requests.post(new_item, headers=headers,data=payload) print(response.status_code) #print(response.text) pretty_json = json.loads(response.text) print (json.dumps(pretty_json, indent=2)) except exception as e: print(str(e)) modify item this api allows you to modify an in-app item without updating the app. the item id can't be changed using this api. it is recommended to use the response from the view individual item api to create the input required for this request. in this example, i modify the title and description of an item which i created in the previous section. i also modify the usd price, local price, country id, and currency values. i create a dictionary to hold the data which is sent to the api, as was done in the previous section. # data to be sent payload = json.dumps({ "id": "one_gallon_gas", "title": "3 gallon gas", "description": "modify fuel for driving game fix", "type": "consumable", "status": "published", "itempaymentmethod": { "phonebillstatus": true }, "usdprice": 1, "prices": [ { "countryid": "kor", "currency": "krw", "localprice": "900" }, { "countryid": "usa", "currency": "usd", "localprice": "1" } ] }) since this request is for modification, i send it using a put request, which is a method for updating the entire resource. so, i define the url and send the http request. # defining the api endpoint modify_item = "https://devapi.samsungapps.com/iap/v6/applications/" + packagename + "/items" try: response = requests.put(modify_item, headers=headers,data=payload) print(response.status_code) pretty_json = json.loads(response.text) print (json.dumps(pretty_json, indent=2)) except exception as e: print(str(e)) partial item modification this api is used to modify the title, countryid, or localprice of an in-app item. i can't change the item id using this api. this api only does a partial update unlike the modify item api, so an http patch request is used. the patch request requires only those fields which are to be modified. it doesn't change the other fields of the resource, in contrast to the put request which changes the entire resource. in this example, i modify only the title of the 'one_gallon_gas' item. the python script remains the same as in the previous section. # data to be sent to the api payload = json.dumps({ "id": "one_gallon_gas ", "title": "5 gallon gas" }) # defining the api endpoint partial_modify_item = "https://devapi.samsungapps.com/iap/v6/applications/" + packagename + "/items" try: response = requests.patch(partial_modify_item, headers=headers,data=payload) print(response.status_code) pretty_json = json.loads(response.text) print (json.dumps(pretty_json, indent=2)) except exception as e: print(str(e)) remove item this api allows us to remove an in-app item. here, the item id is the required field in the request url. i add the item id in the request url and send the http delete request. the delete method is used to request the server to delete a resource specified by the given url. itemname = "one_gallon_gas" # defining the api endpoint remove_item = "https://devapi.samsungapps.com/iap/v6/applications/" + packagename + "/items/" + itemname payload = {} try: response = requests.delete(remove_item, headers=headers,data=payload) print(response.status_code) pretty_json = json.loads(response.text) print (json.dumps(pretty_json, indent=2)) except exception as e: print(str(e)) figure 3: response of the remove item api conclusion other than selling paid apps, in-app purchases are the most common and popular way to monetize an app. you can integrate the samsung in-app sdk in your app to generate profit and use the iap publish api to automate item-related tasks. this is the last blog article of this series. i hope you've found this article helpful. you can check out the other blogs about the galaxy store developer api below. blog articles in this series how to create an access token for the galaxy store developer api using python: learn how to create an access token using the galaxy store developer api and python. how to get statistics: galaxy store statistics (gss) is a tool that provides information about your sales, revenue, ratings, and much more, which helps you to make more effective business decisions. you can learn how to get statistical data from your apps using python. how to manage content: manage your content in galaxy store by using the content publish api. in an app production system, managing content is a frequent task. this blog covers the implementation of this api using python to automate your app distribution system.