Help articles

How to use Insomnia

TI provides all of our APIs as OpenAPI documents that can be imported into tools such as Insomnia. Insomnia can be downloaded for free at https://insomnia.rest/.   
 

  • Open Insomnia
  • Create a new project
  • Click on create > Request collection 
  • Import the TI OpenAPI file
  • Go to Mange environments
    • Ensure that your base environment contains the following variable.

                            {
                            "base_url": "{{ scheme }}://{{ host }}{{ base_path }}"
                            }

  • After updating the base environment select the transact.ti.com environment.
  • Update "oauth2ClientId with your TI store API key and oauth2ClientSecret with your TI store API secret.


How to use code snippets

Code snippets that can be used in your implementation can be generated in many different languages for TI APIs using the OpenAPI specifications embedded in documentation of the TI API portal or using the Insomnia tool. 

From the TI API portal:

  • Go to the desired API page.
  • Expand one of the methods.
  • Click on "try it out."
  • Click on "Execute."
  • A tab page of code snippets will open up.

From Insomnia:
 

  • After importing the collection Into insomnia, select the method and click on the down arrow to the right.
  • Select "Generate Code."

 

VB.Net example

Framework 4.6.1

Imports System.Net
Imports System.IO
Imports System.Text
Imports Newtonsoft.Json.Linq


Public Class RESTAPI
    Public ClientID As String
    Public ClientSecret As String
    Private MyToken As RESTAPI.Token
    Public Function GetPricing(TiPartNumber As String) As String
        If IsNothing(MyToken) Then
            GetToken()
        Else
            If MyToken.IsValid = False Then
                GetToken()
            End If
        End If

        Return GetWithOAuth2($"https://transact.ti.com/v2/store/products/{TiPartNumber}?currency=USD&exclude-evms=true", MyToken.Token)


    End Function
    Private Sub GetToken()

        Dim URL As String = "https://transact.ti.com/v1/oauth/accesstoken"

        Dim Data As String = $"grant_type=client_credentials&client_id={ClientID }&client_secret={ClientSecret}"
        Dim request As HttpWebRequest = DirectCast(WebRequest.Create(URL), HttpWebRequest)
        request.Method = "POST"
        request.ContentType = "application/x-www-form-urlencoded"   
        Dim byteArray As Byte() = Encoding.UTF8.GetBytes(Data)
        request.ContentLength = byteArray.Length
        Dim dataStream As Stream = request.GetRequestStream()
        dataStream.Write(byteArray, 0, byteArray.Length)
        dataStream.Close()
        Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
        dataStream = response.GetResponseStream()
        Dim reader As New StreamReader(dataStream)
        Dim result As String = reader.ReadToEnd()
        reader.Close()
        dataStream.Close()
        response.Close()

        Dim parsejson As JObject = JObject.Parse(result)
        MyToken = New Token With {.Time = Now,
            .Token = parsejson.SelectToken("access_token").ToString
            }

    End Sub

    Public Function GetWithOAuth2(ByVal url As String, ByVal accessToken As String) As String
        Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
        request.Method = "GET"
        request.Headers.Add("Authorization", "Bearer " + accessToken)
        Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
        Dim responseStream As Stream = response.GetResponseStream()
        Dim reader As New StreamReader(responseStream)
        Dim result As String = reader.ReadToEnd()
        reader.Close()
        response.Close()
        Return result
    End Function

    Private Class Token
        Public Token As String
        Public Time As DateTime
        Public Function IsValid() As Boolean
            If DateDiff(DateInterval.Minute, Now, Time) >= 60 Then
                Return False
            Else
                Return True
            End If
        End Function
    End Class

End Class