Creating shipping labels programmatically can significantly streamline the logistics of your business, saving time and reducing errors compared to manual label creation. For businesses using FedEx as a shipping carrier, leveraging their RESTful API for creating shipping labels is an efficient way to integrate shipping capabilities directly into your application.
This guide will walk you through the process of creating a FedEx shipping label using PHP with the FedEx RESTful API.
We’ll cover the necessary steps, from setting up your FedEx developer account to making authenticated API calls to generate and retrieve shipping labels. By the end of this guide, you’ll have a clear understanding of how to automate shipping label creation for your business using FedEx’s powerful API.
How to Get FedEx API Access
To create a FedEx shipping label via their RESTful API, you’ll first need to obtain access to FedEx’s API services. This involves setting up a FedEx developer account and obtaining the API Key and Secret ID. In our previous post, we explained how to get FedEx API and Secret Key.
Generate FedEx shipping label via API in PHP:
Once you have obtained the required FedEx API credentials and set up your development environment, you can start generating shipping labels using PHP. This involves making HTTP requests to FedEx’s RESTful API endpoints. Here’s a detailed guide to help you through the process.
Step 1: Obtain oAuth Token for API Authentication
To generate an OAuth token, you need to send a POST request to the FedEx OAuth service endpoint with your client credentials. Below is a PHP code that performs this task.
You need to add the FedEx developer Client ID and Secret key in the payload for API Authentication.
An error has occurred. Please try again later. |
This will return you valid access token as a response, use this token as a Bearer authentication to create FedEx shipping API calls.
Step 2: Create a FedEx Shipping Label via API in PHP
Once you have obtained the OAuth token, you can proceed to create a shipment using FedEx’s RESTful API. This involves making a POST request to the FedEx Ship API endpoint with the shipment details.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
<?php $paramval = '{ "requestedShipment": { "shipper": { "contact": { "personName": "SHIPPER NAME", "phoneNumber": 1234567890, "companyName": "Shipper Company Name" }, "address": { "streetLines": [ "SHIPPER STREET LINE 1" ], "city": "HARRISON", "stateOrProvinceCode": "AR", "postalCode": 72601, "countryCode": "US" } }, "recipients": [ { "contact": { "personName": "RECIPIENT NAME", "phoneNumber": 1234567890, "companyName": "Recipient Company Name" }, "address": { "streetLines": [ "RECIPIENT STREET LINE 1", "RECIPIENT STREET LINE 2" ], "city": "Collierville", "stateOrProvinceCode": "TN", "postalCode": 38017, "countryCode": "US" } } ], "shipDatestamp": "2024-05-03", "serviceType": "STANDARD_OVERNIGHT", "packagingType": "FEDEX_PAK", "pickupType": "USE_SCHEDULED_PICKUP", "shippingChargesPayment": { "paymentType": "SENDER" }, "labelSpecification": { "imageType": "PDF", "labelStockType": "PAPER_85X11_TOP_HALF_LABEL" }, "requestedPackageLineItems": [ { "weight": { "value": 10, "units": "LB" } } ] }, "accountNumber": { "value": "1234567" } }'; $urlval = 'https://apis-sandbox.fedex.com/ship/v1/shipments'; // Production Url: https://apis.fedex.com/ship/v1/shipments $ch23 = curl_init(); curl_setopt($ch23, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch23, CURLOPT_URL, $urlval); curl_setopt($ch23, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch23, CURLOPT_TIMEOUT, 10); curl_setopt($ch23, CURLOPT_POST, true); curl_setopt($ch23, CURLOPT_POSTFIELDS, $paramval); curl_setopt($ch23, CURLOPT_ENCODING, "gzip"); curl_setopt($ch23, CURLOPT_HTTPHEADER, array( "Content-Type: application/json", "X-locale: en_US", "Authorization: Bearer ACCESS_TOKEN_VALUE" )); $shipresponse = curl_exec($ch23); curl_close($ch23); $ParsedShipresponse = json_decode($shipresponse); echo "<pre>"; print_r($ParsedShipresponse); ?> |
Also Read:
Create Fedex Multiple Package Shipment Label
How to Install Magento Security Patch via Putty
Explanation
- Endpoint URL: The Ship API endpoint for Production is
https://apis.fedex.com/ship/v1/shipments
. - Shipment Data: The request payload includes details about the shipper, recipient, shipment, and packaging.
shipper
: Contains contact and address information for the sender.recipient
: Contains contact and address information for the recipient.shipTimestamp
: Specifies when the shipment is created.serviceType
: Specifies the type of FedEx service (e.g.,STANDARD_OVERNIGHT, FEDEX_GROUND
).packagingType
: Specifies the type of packaging used.shippingChargesPayment
: Specifies who is responsible for shipping charges.labelSpecification
: Defines the format and type of the shipping label.requestedPackageLineItems
: Specifies the details of the package, including weight and dimensions.
Conclusion
This example provides a basic idea for creating a FedEx shipping label using the RESTful API in PHP. Refer to the official FedEx Web Services documentation for comprehensive details and tailor the request to meet your specific requirements.
I have created a FedEx RESTful API Script in PHP which will include these features. You can get it via below download button.
- FedEx API Authorization Code
- Live FedEx Shipping Rates by Postal Code
- Check FedEx Shipping Service Availability by Zip Code
- Create FedEx Shipment
- Track FedEx Shipment via Tracking Number
- FedEx Domestic Express Shipment
- FedEx Ground Return Label
- FedEx Standard Overnight COD
- FedEx Multiple Shipment
- FedEx International Priority
- FedEx 2-Day Signature Required
- FedEx 2-Day Saturday Pickup
- FedEx Shipment With Dangerous Goods
- Parsed Response Data
Appreciate the code, but generated pdf files were of zero kb, any idea?
Hello Kausik,
Which service type you use for generate shipping label ? Can you share your soap request code ?
generated pdf files 0 kb, can you help, please
Hi Ahmad,
I think you should check the error response first. If you getting label image data in response than it should be problem in convert it into an image.
Hi Harish,
I simply uploaded ShipService_v17.wsdl and label.php with your code under a folder and tried to execute the php file. Do I need to do any thing else? If you share your email id, I can share test key and other details.
Have you change shiptimestamp with next shipping date in request ? Still facing issue, mail me info on this id “[email protected]”
Hi again,
in your code what are parent and user credential? IN FEDX php code also I don’t see any such reference. Can you please explain?
$request[‘WebAuthenticationDetail’] = array(
‘ParentCredential’ => array(
‘Key’ => ‘EPLkBjBoWJL4gWDz’,
‘Password’ => ‘6RrOFaG3V9hP2tRwll58Tunb0’
),
‘UserCredential’ => array(
‘Key’ => ‘EPLkBjBoWJL4gWDz’,
‘Password’ => ‘6RrOFaG3V9hP2tRwll58Tunb0’
)
Hi, both takes same credential.
Dear Harish,
I mailed you the file with code in the morning, did you get chance to check it?
Regards,
Kausik
Hi Kausik,
I tried your code with my fedex test account access. Its work fine. You should change Shipper Address and Recipient address by own developer test address.
When you create fedex development key, they will mail you test address for using fedex services.
Hi Harish,
I just mailed you, kindly check.
Hello, i just email you working code. Just edit shipper and recipient address and its work fine.
sir i need some help to integrate fedex api to my website
,i want to check availiability of service of fedex depend on postal code
Hello sandeep,
I think for this you need to run Fedex Postal Code Inquiry api. Which give result shipping available on particular post code or not.
I used same Test key & password in credentials
but I am getting this error
Severity: ERROR
Source: prof
Code: 1000
Message: Authentication Failed
Hi Tut,
You must use Test Development Access, which you got at the time of registration in fedex account.
Also for label request change shipper and recipient address as per your fedex account.
That worked. Thanks for you help.
Hello Harish ,
I did all chages suggested by you still it shows authentication failed error.Please check my attached file on your mail and suggest me appropriate changes.
Which account number is used in script?
That is define in Test password mail.Or that are defined in shipper and recipient address.
Hi Rohit,
Authentication error comes when you trying to create fedex label using production account access without evaluation process. Are you using right developer access in it ?
Account number you received with Password email from Fedex.
Is it require to enable test password of web services on fedex?
No, its enable by default.
Still i facing same error. please check my script on your mail.
Ok i will check, can you also send me your test access to check ?
Please keep thrwiong these posts up they help tons.
What is difference between ParentCredential and USER credentials?
Both will take same credential.
Hi Harish,
Hello Harish,
I just mailed you the new file with script , did you get chance to check it?
Hi Harish,
I am doing integration of fedex api in php, I use rate available api to get list of services. But can you help to for next step. i think we have to place shipping order to get tracking code.
So, it will be helpful if you provide some information on it.
Hi sam,
You have to write Fedex Shipping label code to get tracking code. Just place a test order with fedex service type to create shipping label. I hope this post will help to do this.
Thanks
Thanks for your prompt reply, as in create shipment there are different folders and each have shipment file init. so which one i use as i want to create shipment for india. (like: form india to india).
Yes exactly , i also wants same.. when using india to india address,, fedexp apj not supported at all.
Hello ,
Which Account number used in API credential?
Hi,
Use account number your got in Developer Test Key Registration email from Fedex.
Hello Harish,
Please check my email.
Hello Harish ,
Anything wrong in the script.That sent via mail.
Hello Harish,
Plz give me some suggestions for NON COD basis labels.
Hi Rohit,
If you are using FEDEX_GROUND service type than COD parameter is required in label request. For print Non COD Fedex label for other service, just remove these request parameter from label request code.
‘SpecialServicesRequested’ => array(
‘SpecialServiceTypes’ => ‘COD’,
‘CodDetail’ => array(
‘CodCollectionAmount’ => array(
‘Currency’ => ‘USD’,
‘Amount’ => ‘10.0’
),
‘CollectionType’ => ‘ANY’
)
)
I have used this code , but when i enter indian address . i have received following error;
[Notifications] => stdClass Object
(
[Severity] => ERROR
[Source] => ship
[Code] => 2449
[Message] => Customs Clearance Detail is required
[LocalizedMessage] => Customs Clearance Detail is required
)
but when i use outside india .. its working ..
so my concern is that , why i receiving that customclearance error in india address.
Hi Piyush,
Fedex have different policies for each country. If custom clearance is required for India country than you should pass those parameters in API request. This given code belong to US country.
Thanks
Hello , Could you please send me code that is related to india.. ?
Hello , Could you please send me code that is related to india.. ? please send Label.php according to india shipping label format.
Well, currently i don’t have Fedex shipping code for India country basis. You should check Fedex api documentation to create an request for it.
Hi Harish,
Where can i get the source ‘ShipService_v19.wsdl’ ? Please
let me know.
Thanks for your help
Hi Ramesh,
You can download all shipping source files from Fedex account. Their are all Fedex web service source and documentation available.
For now you can download wsdl file from here : https://codefixup.com/fedex/ShipService_v19.wsdl
Hi,
This code really help me to fix shipping api call. Thanks and very much appreciated.
I have used your code but I am unable to generate label. So can you please let me know how can I generate the fedex label in png format?
Hi,
Have you check label generation API call response. Should cross check it, it must parameter issue.
For png label, change parameter value in LabelSpecification parameter. Follow Fedex official developer documentation for valid parameters and corresponding values.
Thanks
Hello Harish,
have you script for Fedex Tracking API in php.
Please let me know.
Yes i can create script for that.
Thanks
Can you please send me the full working code on my email.I am new in Apis integration .
Thanks In Advance.
🙂
Hi Karan,
I have explained here how to execute FedEX api call and print shipping label. You should follow fedex official documentation to create API request as per your need. If you need my service to create FedEX APIs call, inbox me on [email protected].
Thanks
Function (“processShipment”) is not a valid method for this service
Hi Mustafa,
This is a valid function, above mentioned Fedex API code is valid. I have tested so many times.
What issue you facing ? Please tell me.
Thanks
Hi,
I sadly get empty PDF Files (0 kb). Whats wrong? 🙁
Best Regards
Philipp
Hi Philipp,
I think you should check the response first. Something not right in your API request. In API response you will get the error detail.
Just print_r($response); this.
Thanks
Thanks for the hint! 🙂 There was something wrong with the Zip Code. Now it works.
Best Regards
Philipp
OK great…
Hello,
I am new for fedex system. I want to implement fedex API for generate shipping label. Can you provide me working code for testing purpose.
Please reply me as soon as possible.
Thanks
Ansari Umair
Hi Umair,
I have mentioned working code here. You can implement using it. What problem you facing to use it ?
If you need my service to create fedex shipping label code, please inbox me : [email protected]
Thanks
Hello,
I am running into an issue with creating a Return Label. I am getting PHP errors rather than it giving me XML output error. Uncaught SoapFault exception: [SOAP-ENV:Server] SoapClient->__call(‘processShipment’, Array)
All I changed was this:
‘SpecialServicesRequested’ => array(
‘SpecialServiceTypes’ => ‘RETURN_SHIPMENT’,
‘ReturnShipmentDetail’ => array(
‘ReturnType’ => ‘FEDEX_TAG’,
‘Rma’ => array(
‘Number’ => ‘012’,
‘Reason’ => ‘reason’
)
)
)
I also tried this:
‘SpecialServicesRequested’ => array(
‘SpecialServiceTypes’ => array(‘RETURN_SHIPMENT’),
‘ReturnShipmentDetail’ => ‘ReturnType’,
‘Value’ => ‘PRINT_RETURN_LABEL’
)
Hi Mr. Harish can you guide me how to pass multiple package shipment??
Hi Dhananjay,
I already written an post regarding create fedex multiple package shipment. Have you check this : https://www.codefixup.com/how-to-create-fedex-multiple-package-shipment-label-using-api-call-in-php/ ?
Hi,
I have liked the page on facebook for unblock the Label.php content, But Still I am not able to see the content code of Label.php, So can you please provide the code for Label.php.
Thanks.
I need to print fedex shiping labels on click. Can you send me code files please?
Hi Vince,
I already mentioned code details above, you can make your own script using it. If you facing issues, i will make Fedex API script for you, but this is paid service.
Thanks
Hi Harish,
thanks for your great script. we have extended the script that we can generate commercial invoices for international shipments. we would like to integrate our own company logo like it is when we generate the invoice by the fedex webpage.
do you have any sample code or a tip?
Thanks.
Hi,
I don’t have ready code for this, but I think you can do this in two ways, First from the FedEx account setting and second you have to find the relevant parameter in shipment create API documentation for this.
Hi Harish
I tried your code but getting some issues regarding credentials showing when i tried to call file on the frontend.
I had sent you a mail regarding this issue yesterday.pls review that and help me
ERROR: SOAP Fault: (faultcode: Client, faultstring: SOAP-ERROR: Encoding: object has no ‘UserCredential’ property) in /var/www/html/Label.php on line 140
Thanks
Hi Vivek,
I think you missed User Credential parameters in Soap API request, please check official documentation.
Hi Harish,
I tried your code and a response is coming but the label image is not generated.
ex:
[Label] => stdClass Object
(
[Type] => OUTBOUND_LABEL
[ShippingDocumentDisposition] => RETURNED
[ImageType] => PDF
[Resolution] => 200
[CopiesToPrint] => 1
[Parts] => stdClass Object
(
[DocumentPartSequenceNumber] => 1
[Image] => %PDF-1.4
Thanks
Hi Komal, i think this is becuase of incorrect parameters request. Please cross check it again Or i suggest to buy working Fedex Shipping Label code from my website.
Hi Harish,
I tried your code and a response is coming and the image also generating but getting a warning
“Unable to obtain courtesy rates”.
pls, help me on this why it is showing warning earlier it does not show any warning.
stdClass Object
(
[HighestSeverity] => ERROR
[Notifications] => stdClass Object
(
[Severity] => ERROR
[Source] => ship
[Code] => 2467
[Message] => shippingChargesPayment is required
[LocalizedMessage] => shippingChargesPayment is required
)
[TransactionDetail] => stdClass Object
(
[CustomerTransactionId] => ProcessShip_Basic
)
[Version] => stdClass Object
(
[ServiceId] => ship
[Major] => 19
[Intermediate] => 0
[Minor] => 0
)
)
Hello please tell me what should i do
what i miss
Hi, you have missed ‘ShippingChargesPayment’ parameter in create shipment API request. Use below parameters:
‘ShippingChargesPayment’ => array(
‘PaymentType’ => ‘SENDER’,
‘Payor’ => array(
‘ResponsibleParty’ => array(
‘AccountNumber’ => ‘********’, // enter your Fedex Account Number
‘Contact’ => null,
‘Address’ => array(
‘CountryCode’ => ‘IN’
)
)
)
)