Instant API works in Notebook but fails when deployed.

So I made a function in a notebook like so and it worked.

func = APIFunction[{"base" -> "Integer" , "digits" -> "Integer"}, BaseForm[N[Pi,#digits],#base]&];
func[<|"base" -> 5 , "digits" -> 20|>] 

output = 3.032322143033432411241224042

When I deploy it to the cloud using CloudDeploy[func] and then use the params ?digits=20&base=5

I get the following from the API

BaseForm[3.1415926535897932384626433832795028841971693993751058209749`20., 5]

Am I doing something wrong?

Thanks

Hi,

did you set the Permissions to “Public”?

I tried

CloudDeploy[ APIFunction[{"base" -> "Integer" , "digits" -> "Integer"}, BaseForm[N[Pi,#digits],#base]&];func[<|"base" -> 5 , "digits" -> 20|>]         ,Permissions -> "Public"]

and got a link to this website:

Cheers,
Marco

I set it to Public and i’m still having an issue. Also Mario i’m trying to pass in the parameters to the API , not a static response.

What is the full CloudDeploy function call (with options) you used?

CloudDeploy[ APIFunction[{“base” → “Integer” , “digits” → “Integer”}, BaseForm[N[Pi,#digits],#base] &], Permissions → “Public”]

I checked with developers on this and found out that BaseForm assumes a Mathematica-like front end.

That is why it works in Wolfram Programming Cloud.


BaseForm is interpreted by the FE to print the number in that form in a notebook. By default CloudDeploy returns Wolfram Language so the result he is seeing isn’t formatted because it is WL that is not displayed in a notebook. I tried

CloudDeploy[ APIFunction[{"base" -> "Integer" , "digits" -> "Integer"}, BaseForm[N[Pi,#digits],#base] &,"HTML"]]

with HTML output and got closer to the expected result, but the formatting isn’t perfect. This format is probably closer to the user intention, though.


Nice, I think that is good enough for what I had in mind.

Thanks Bruce!

According to the documentation, BaseForm is interpreted by the notebook interface to display a certain way, but it does not affect evaluation. When you CloudDeploy an APIFunction, its output format is Wolfram Language by default, so you’re seeing what’s expected.

There are several options, depending on what you’re going for:

  1. Give the output as an image, specifying the “PNG” format as a second argument to APIFunction:

     CloudDeploy[APIFunction[{"base" -> "Integer", "digits" -> "Integer"},
         BaseForm[N[Pi, #digits], #base]&,"PNG"]]
    
  2. Make the output text format by running BaseForm through ToString, and specifying “Text” format. The base is put on a second line to show it as a subscript:

     CloudDeploy[APIFunction[{"base" -> "Integer", "digits" -> "Integer"},
         ToString[BaseForm[N[Pi, #digits], #base]]&,"Text"]]
    
  3. If you want to return a text response giving just the numeric value without showing the base, you could use RealDigits.

For readability, first we break out a function piDigits:

piDigits[base_, digits_] :=
   RealDigits[N[Pi, digits], base] /. {
      {rdigits_List, nleft_} :> StringJoin@{ToString /@ Take[rdigits, nleft], ".", ToString /@ Drop[rdigits, nleft]}
   }

Then we deploy an API based on it that returns Text:

CloudDeploy[APIFunction[{"base" -> "Integer", "digits" -> "Integer"}, piDigits[#base, #digits]&, "Text"]]

Marco, what you’re seeing there isn’t a result of setting Permissions->“Public”. Note that your APIFunction expression ends in a semicolon. What you actually ended up CloudDeploying is the result of calling func with some arguments, which is a simple expression. By default, CloudDeploy of an expression is rendered as a notebook as you showed.

I am having a similar issue with “Import.” For some reason I can’t get it to work as an Instant API. I tried CloudImport as well. Any ideas?

My Code:

CloudDeploy[
 APIFunction[
  "urlTargets" -> "String" -> "http://www.businesstexter.com",
  func2 = data = Import[#urlTargets, "Hyperlinks"]
     &]
 ]

It looks like Import from a URL is not working in an API due to a bug, an overzealous security setting. We’ll get that fixed soon.

Awesome! I thought it was just me sucking at learning this new WL. :slight_smile: lol

Seems like its fixed. :slight_smile: