FedEx Tracking API Example
python - Utilizing FedEx Track API to capture Tracking Status results in Status 422 - 'INVALID.INPUT.EXCEPTION', 'message': 'Invalid field value in the input' - Stack Overflow
FedEx Tracking
Getting full tracking details from Fedex API
Videos
Hello,
This is some sample code I've written for using the FedEx tracking API via PowerShell. I posted something similar awhile back for UPS, but recently had a business need that required me to figure out FedEx tracking too (If anyone knows how to do the same with Canadian carrier Purolator, let me know).
First off, before you can do anything, you need to sign-up in the FedEx Developer Portal at https://developer.fedex.com/api/en-us/home.html
You'll need to create or join an organization and the organization must have at least one active FedEx account linked to it.
Once you've done all that you'll be able to create a new project and get an API key and Secret key to generate an Oauth token used for authentication. Best to just read the documentation in the portal for that part.
Now for the PowerShell. I've created two functions. The first "Get-FedExToken" is used to generate an Oauth token. That token will be used for all your queries using the second function.
The second function is Invoke-FedExRestMethod. This is basically just a wrapper for the Invoke-RestMethod cmdlet with some info filled out already.
# Generates a Oauth token for authentication.
Function Get-FedExToken{
param(
$ClientID,
$ClientSecret
)
$RestMethodParams = @{
URI = "https://apis.fedex.com/oauth/token"
Method = "POST"
Headers = @{
"Content-Type" = "application/x-www-form-urlencoded"
}
Body = "grant_type=client_credentials&client_id=$ClientID&client_secret=$ClientSecret"
}
Invoke-RestMethod @RestMethodParams
}
# Custom wrapper for Invoke-RestMethod.
Function Invoke-FedExRestMethod{
param(
[String[]]$TrackingNumber,
$Token
)
$BODY = [PSCustomObject]@{
trackingInfo = [PSCustomObject[]]@{
trackingNumberInfo = [PSCustomObject]@{
trackingNumber = $TrackingNumber
}
}
includeDetailedScans = $true
} | ConvertTo-Json -Depth 3
$RestMethodParams = @{
URI = 'https://apis.fedex.com/track/v1/trackingnumbers'
Method = 'POST'
Headers = @{
"content-type" = "application/json"
authorization = "bearer $($Token.access_token)"
}
Body = $BODY
}
Invoke-RestMethod @RestMethodParams
}
# Example Usage:
$ClientID = '<ID>'
$ClientSecret = '<SecretKey>'
$Token = Get-FedExToken -ClientID $ClientID -ClientSecret $ClientSecret
$Result = (Invoke-FedExRestMethod -TrackingNumber '999999999999' -Token $Token).output
$Result.completeTrackResults.trackResultsUse valid values for includeDetailedScans: True/ False. Then serialize the payload, and will be fine.
authoriztion_key = get_oauth()
url = f"{sandbox_url}/track/v1/trackingnumbers"
headers = {
'Content-Type': "application/json",
'x-locale': "en_US",
'Authorization': f'Bearer {authoriztion_key}'
}
payload = {
"trackingInfo": [
{
"trackingNumberInfo": {
"trackingNumber": f"{tracking_number}"
}
}
],
"includeDetailedScans": True
}
payload = json.dumps(payload)
response = requests.post(url, data=payload, headers=headers)
includeDetailedScans:Indicates if detailed scans are requested or not. Valid values are True, or False.
Did you try giving it a boolean value instead of an int?