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.

If you're not already familiar with how to use OpenAPI specification files to generate clients or how to install and use them, LLMs like ChatGPT can be very helpful with navigating the unique aspects and build tools for your programming language.

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.

Your client secret will only be shown to you once so be sure to save it somewhere. Don't worry though, if you lose it you can always generate a new Client or refresh your secret.

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-generator

Next, 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_client

For additional approaches to install and generate clients using the openapi-generator tool, such as with Windows, please refer to the documentation.

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.

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:

Note that in the example above, the status variable is using a type also imported from our generated code. Finding and importing these types can be much easier if you use an IDE such as PyCharm in our case here. The generated client resource will be expecting specific types for its arguments.

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?