How to Use OpenAPI Clients with Super SIM
Learn how to use KORE's OpenAPI specification file to generate an API client that you can use to make calls to the Super SIM APIs.
In this guide, we're going to build a python script that API clients built from our OpenAPI specification file. The tool used below to generate the client, openapi-generator, supports many different programming languages, not just python.
Create Your KORE API Client Resource
To make requests to the KORE REST APIs, you need to first create a Client resource in the KORE Developer Portal. Note this is different than the "clients" that we'll be generating from the OpenAPI specification file and will be using to make requests to the APIs. You get your necessary credentials by creating this Client resource.
KORE's API's follow OAuth2 for authenticating to the APIs. You'll create a Client that includes Super SIM within its scoped products and then use the ID and secret of the Client to generate an access token that you'll include in your request headers to authenticate to the API endpoints.
Learn more about how to generate this Client to use with Super SIM here. You can jump to "Get Started!" section here for the steps to create your Client. Once you have your client ID and secret return here.
Generating Your Local Client
There are many options you can choose from for to generate API clients from OpenAPI specification files that may work better for your development environment or that use syntax and patterns that you may find easier to understand. How you install and use them and then use the generated clients will vary significantly from tool to tool. For this guide we'll be using the openapi-generator CLI tool.
First, install the openapi-generator CLI tool.
brew install openapi-generatorNext, download a copy the Super SIM OpenAPI specification file from Github and save it in your project directory.
Generate a python client using the OpenAPI specification file and the generator tool.
openapi-generator generate -i ./kore-supersim-v1.json -g python -o ./clients/kore_supersim_client --package-name supersim_clientFrom this point forward, all the code and commands will be specific to Python3. Except for the final section, where we provide a comprehensive example of using multiple endpoints to generate a CSV file, we've minimized the use of Python-specific patterns or tricks. This approach makes it easier for those less familiar with Python to follow along.
We understand that not everyone is familiar with Python. If you find any sections of this guide or code difficult to follow, please use the "Was this helpful?" tool on this page and leave a comment. Your feedback helps us improve, and we'll do our best to add more clarifications or comments to the code to explain what is being done.
Create a python virtual environment to which you'll install your packages including your generated client.
Activate the virtual environment.
Install the package locally in your virtual environment.
With that done, let's generate a file called client-test.py and make sure that we can successfully import the installed client and run the code without errors.
Run your code.
If you see "hello world" successfully logged with no errors, then you should be all set to begin making API requests with your generated and installed client. We'll be doing all coding for this guide in the client-test.py file.
Install Other Dependencies
Authenticating to the KORE REST API
To access the KORE REST APIs, we'll need an access token. We'll use the client ID and secret from the Client resource you created earlier to generate a temporary token used to authenticate your requests.
We'll add a function to our code to get a token with our secrets and use it to print out the some useful properties from the access token response. Replace <YOUR_CLIENT_ID> and <YOUR_CLIENT_SECRET> with your unique credentials.
Treat your client ID, secret, and access tokens like they are passwords. Do not share them with anyone and never commit them to any public code repositories.
Access tokens are temporary. You can see how many seconds the token is valid for by checking the expires_in property the response. If you use an expired token, your requests will be rejected. When you get a new token, you'll need to regenerate the clients with a new configuration that has the updated token. We recommend creating a class that manages refreshing the token and regenerating the clients that wraps around the generated client resources in a production setting where your code is deployed and running on servers versus writing simple scripts like we are here.
Generate API Client Instances for Each Endpoint
Now, we'll use our access token to begin generating instances of our API clients, one for each endpoint we'll be making requests to. Note that there is a module to be imported for each endpoint that you'll use to generate a client for that endpoint.
You can use the clients generated for each endpoint to list out your Sims and Fleet resources.
Go ahead and run your code.
Assuming you already have SIMs and Fleets created on your account, you should see arrays of these resources printed out when you run your code. If you have more than the default page size (50) of either resource though, these results are incomplete. That's because the results are just one page. You can increase the page_size up to 1,000 if you have less than 1,000 resources, but to properly scale this out, we'll need to use the pagination information in our responses to fetch all the pages of results we need.
Here's an example of how you could write a function to fetch all pages of Sim resources:
We can now use this function in our main()call to get all of our Sims if we needed to iterate over each one to pull data usage data for each or build CSV export.
Bringing It All Together
Now that we can fetch all of our Sims, we can build upon our script here to build a practical application that you can use easily pull data about all of your SIMs and data usage for the last 30 days.
First, we'll create a similar function to like we did for our Sim resources to fetch all of our Fleet resources.
Next, we're going to generate another endpoint client for the UsageRecords endpoint.
Lastly, we'll iterate over each SIM and pull the SIM's usage and build a CSV report.
If you run your code, you should see the generated CSV in your project directory with information about each SIM and its usage.
Last updated
Was this helpful?