How to Print Fedex Shipping label via API call in PHP

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.

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.

Also Read:

Create Fedex Multiple Package Shipment Label
How to Install Magento Security Patch via Putty

Explanation

  1. Endpoint URL: The Ship API endpoint for Production is https://apis.fedex.com/ship/v1/shipments.
  2. 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

 

Recommended Posts For You

About Harish

I am professional web developer and blogger. Use this blog to share own api and other web development experience with you. I do accept paid work. Write to me at - [email protected]

View all posts by Harish

77 thoughts on “How to Print Fedex Shipping label via API call in PHP”

    1. Hello Kausik,
      Which service type you use for generate shipping label ? Can you share your soap request code ?

        1. 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.

  1. 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.

  2. 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’
    )

  3. Dear Harish,

    I mailed you the file with code in the morning, did you get chance to check it?

    Regards,
    Kausik

    1. 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.

  4. 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

    1. 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.

  5. I used same Test key & password in credentials
    but I am getting this error

    Severity: ERROR
    Source: prof
    Code: 1000
    Message: Authentication Failed

    1. 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.

  6. 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.

  7. Which account number is used in script?
    That is define in Test password mail.Or that are defined in shipper and recipient address.

    1. 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 ?

  8. 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.

    1. 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

      1. 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).

        1. Yes exactly , i also wants same.. when using india to india address,, fedexp apj not supported at all.

    1. 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’
      )
      )

  9. 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.

    1. 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

    1. Hello , Could you please send me code that is related to india.. ? please send Label.php according to india shipping label format.

      1. 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.

  10. 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?

    1. 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

    1. 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

    1. 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

    1. 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

      1. Thanks for the hint! 🙂 There was something wrong with the Zip Code. Now it works.

        Best Regards

        Philipp

  11. 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

    1. 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

  12. 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’
    )
    )
    )

    1. I also tried this:

      ‘SpecialServicesRequested’ => array(
      ‘SpecialServiceTypes’ => array(‘RETURN_SHIPMENT’),
      ‘ReturnShipmentDetail’ => ‘ReturnType’,
      ‘Value’ => ‘PRINT_RETURN_LABEL’
      )

  13. 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.

    1. 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

  14. 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.

    1. 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.

  15. 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

  16. 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

    1. 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.

  17. 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.

  18. 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

    1. 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’
      )
      )
      )
      )

Leave a Comment

Your email address will not be published. Required fields are marked *