Using a third-party FedEx account for generating shipping labels via the FedEx REST API with OAuth 2.0 involves several steps. This process includes obtaining OAuth credentials, authenticating, and making API requests with third-party billing information. Here’s a step-by-step guide on how to achieve this:
Step 1: Obtain FedEx REST API and OAuth 2.0 Credentials
- Register for a FedEx Account: If you don’t have one, register on the FedEx website.
- API Access: Obtain the client ID and client secret for OAuth 2.0 from the FedEx Developer Portal.
Step 2: Get OAuth 2.0 Token
You need to authenticate and get an OAuth 2.0 token before making API requests.
Example Code to Get OAuth 2.0 Token
use GuzzleHttp\Client;
function getFedexOauthToken($clientId, $clientSecret) {
$client = new Client();
$response = $client->post('https://apis-sandbox.fedex.com/oauth/token', [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
],
'auth' => [$clientId, $clientSecret],
'form_params' => [
'grant_type' => 'client_credentials'
],
]);
$data = json_decode($response->getBody(), true);
return $data['access_token'];
}
// Usage
$clientId = 'your_fedex_client_id';
$clientSecret = 'your_fedex_client_secret';
$accessToken = getFedexOauthToken($clientId, $clientSecret);Step 3: Create Shipment Request with Third-Party Billing
Once you have the access token, you can make the shipment request.
Example JSON Payload for Shipment Request
Here’s an example JSON payload for a shipment request using third-party billing:
{
"requestedShipment": {
"shipper": {
"address": {
"streetLines": ["Address Line"],
"city": "City",
"stateOrProvinceCode": "State",
"postalCode": "Postal Code",
"countryCode": "Country Code"
},
"contact": {
"personName": "Shipper Name",
"phoneNumber": "1234567890"
}
},
"recipient": {
"address": {
"streetLines": ["Address Line"],
"city": "City",
"stateOrProvinceCode": "State",
"postalCode": "Postal Code",
"countryCode": "Country Code"
},
"contact": {
"personName": "Recipient Name",
"phoneNumber": "0987654321"
}
},
"shippingChargesPayment": {
"paymentType": "THIRD_PARTY",
"payor": {
"responsibleParty": {
"accountNumber": "Third-Party FedEx Account Number",
"contact": null,
"address": {
"countryCode": "Third-Party Country Code",
"postalCode": "Third-Party Postal Code"
}
}
}
},
"labelSpecification": {
"imageType": "PDF",
"labelStockType": "PAPER_4X6"
},
"serviceType": "FEDEX_GROUND",
"packagingType": "YOUR_PACKAGING",
"packageCount": 1,
"requestedPackageLineItems": [
{
"weight": {
"units": "LB",
"value": 5
},
"dimensions": {
"length": 10,
"width": 5,
"height": 5,
"units": "IN"
}
}
]
}
}Step 4: Send the API Request
Using Guzzle in PHP, you can send this request with the OAuth token.
Example Code to Create Shipment
function createFedexShipment($accessToken, $shipmentData) {
$client = new Client();
$response = $client->post('https://apis-sandbox.fedex.com/ship/v1/shipments', [
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $accessToken,
],
'body' => json_encode($shipmentData)
]);
return json_decode($response->getBody(), true);
}
// Usage
$shipmentData = [
"requestedShipment" => [
"shipper" => [
"address" => [
"streetLines" => ["Address Line"],
"city" => "City",
"stateOrProvinceCode" => "State",
"postalCode" => "Postal Code",
"countryCode" => "Country Code"
],
"contact" => [
"personName" => "Shipper Name",
"phoneNumber" => "1234567890"
]
],
"recipient" => [
"address" => [
"streetLines" => ["Address Line"],
"city" => "City",
"stateOrProvinceCode" => "State",
"postalCode" => "Postal Code",
"countryCode" => "Country Code"
],
"contact" => [
"personName" => "Recipient Name",
"phoneNumber" => "0987654321"
]
],
"shippingChargesPayment" => [
"paymentType" => "THIRD_PARTY",
"payor" => [
"responsibleParty" => [
"accountNumber" => "Third-Party FedEx Account Number",
"contact" => null,
"address" => [
"countryCode" => "Third-Party Country Code",
"postalCode" => "Third-Party Postal Code"
]
]
]
],
"labelSpecification" => [
"imageType" => "PDF",
"labelStockType" => "PAPER_4X6"
],
"serviceType" => "FEDEX_GROUND",
"packagingType" => "YOUR_PACKAGING",
"packageCount" => 1,
"requestedPackageLineItems" => [
[
"weight" => [
"units" => "LB",
"value" => 5
],
"dimensions" => [
"length" => 10,
"width" => 5,
"height" => 5,
"units" => "IN"
]
]
]
]
];
$response = createFedexShipment($accessToken, $shipmentData);
print_r($response);