170

I've been scouring the net, and can't seem to wrap my head around the idea of a callback URL. In my case I have a few callback URLs that I have to define myself. A popular one is a "default callback URL". What is this exactly? Can you give an example in plain english?

8 Answers 8

120

A callback URL will be invoked by the API method you're calling after it's done. So if you call

POST /api.example.com/foo?callbackURL=http://my.server.com/bar

Then when /foo is finished, it sends a request to http://my.server.com/bar. The contents and method of that request are going to vary - check the documentation for the API you're accessing.

7
  • 1
    Would it cause an error if the callback URL is not set correctly? Feb 5, 2016 at 0:34
  • @user3768495 I would expect it would not, but that's going to depend on the implementation of the API. It could, in theory, make the callback part of a transaction internally, and roll back any changes made by the original request if the callback doesn't return a 2xx response.
    – Eric Stein
    Feb 5, 2016 at 2:50
  • Do I need to send a response to the POST request at /api.example.com/foo ? Jul 5, 2016 at 1:51
  • @Abhijeet You never need to. If it's appropriate to include a response, yes, do so.
    – Eric Stein
    Jul 5, 2016 at 13:53
  • similar question: stackoverflow.com/questions/28391359/…
    – Deke
    May 31, 2018 at 15:42
104

Think of it as a letter. Sometimes you get a letter, say asking you to fill in a form then return the form in a pre-addressed envelope which is in the original envelope that was housing the form.

Once you have finished filling the form in, you put it in the provided return envelope and send it back.

The callback URL is like that return envelope. You are basically saying, "I am sending you this data; once you are done with it, I am listening on this callback URL waiting your response." So the API will process the data you have sent then look at the callback to send you the response.

This is useful because sometimes you may take ages to process some data and it makes no sense to have the caller wait for a response. For example, say your API allows users to send documents to it and virus scan them. Then you send a report after. The scan could take maybe 3 minutes. The user cannot be waiting for 3 minutes. So you acknowledge that you got the document and let the caller get on with other business while you do the scan, then use the callback URL when done to tell them the result of the scan.

0
34

If you use the callback URL, then the API can connect to the callback URL and send or receive some data. That means API can connect to you later (after API call).

Example

Diagram

  1. YOU send data using request to API
  2. API sends data using second request to YOU

Exact definition should be in API documentation.

0
20

It's a mechanism to invoke an API in an asynchrounous way. The sequence is the following

  1. your app invokes the url, passing as parameter the callback url
  2. the api respond with a 20x http code (201 I guess, but refer to the api docs)
  3. the api works on your request for a certain amount of time
  4. the api invokes your app to give you the results, at the callback url address.

So you can invoke the api and tell your user the request is "processing" or "acquired" for example, and then update the status when you receive the response from the api.

Hope it makes sense. -G

0
14

I'll make this pretty simple for you. When a transaction is initiated, it goes under processing stage until it reaches the terminal stage. Once it reaches the terminal stage, the transaction status is posted by the payment gateway to the callback url which generally the merchants use as a reference to show the success/failure page to the user. Hope this helps?

0
9

Another use case could be something like OAuth, it's may not be called by the API directly, instead the callback URL will be called by the browser after completing the authencation with the identity provider.

Normally after end user key in the username password, the identity service provider will trigger a browser redirect to your "callback" url with the temporary authroization code, e.g.

https://example.com/callback?code=AUTHORIZATION_CODE

Then your application could use this authorization code to request a access token with the identity provider which has a much longer lifetime.

7

Here's a common example of how a callback works - something that most people who've bought online probably have experienced. You've made a purchase on your favorite website and clicked the "Submit" button. Chances are you'll be sent to an external service that handles the payment processing (like PayPal) where you confirm your order, or just wait while you watch the word "processing..." blink on the screen. After the transaction completes you are directed back to the original shopping site. The page that you see is the callback URL. Though you didn't know it, you left your favorite online store's website during the processing, and the external site you visited returned you back to it. Barring any glitches you should see the page the store wanted you to see after an order. It might be a thank you page, or other page with order confirmation information, next steps, or their home page. It may vary depending on what you ordered, when you ordered, and how you ordered.

Basically a callback URL gives directions to an external system on where to go next. What is at that URL could be anything. It doesn't have to be a static URL. More

0

In the OAuth workflow, Callback URL would be the url to which the user will be redirected after authorisation from the OAuth provider. Like how 'Sign in with Google' takes you to a Google page and then sends you back to the app where you clicked the 'Sign in with Google' Button.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.