Live data from Hacker News

Ask HN: Best practices (and examples) for designing client libraries for APIs?

news.ycombinator.com

11–20 of 21 posts

Re: Ask HN: Best practices (and examples) for designing client libraries for APIs?

#11
post #6

Stripe has probably the best and cleanest API design.

And documentation. I would spend some time really understanding their docs and client libraries if you want to see a real world example of the most developer friendly docs available.

https://stripe.com/docs/api

https://stripe.com/docs/api/balance/balance_object?lang=pyth...

Re: Ask HN: Best practices (and examples) for designing client libraries for APIs?

#12

Personally, for building a client library I'd take a look at OpenAPI Generator ( https://github.com/openapitools/openapi-generator ) or Swagger Codegen ( https://github.com/swagger-api/swagger-codegen ) - especially if the API provider has an official OpenAPI/Swagger specification. (If not you can always write a specification yourself). Both tools support PHP among other languages.

The clients look terrible and awkward to use thought.

Re: Ask HN: Best practices (and examples) for designing client libraries for APIs?

#13

Personally, for building a client library I'd take a look at OpenAPI Generator ( https://github.com/openapitools/openapi-generator ) or Swagger Codegen ( https://github.com/swagger-api/swagger-codegen ) - especially if the API provider has an official OpenAPI/Swagger specification. (If not you can always write a specification yourself). Both tools support PHP among other languages.

I can't recommend OpenAPI/Swagger-based code generation at all based on my experience working with it. I've only used it for Java, Kotlin and TypeScript, but the generated code (usually more then you asked for) only works like 90% of the time and getting it to 100% takes workaround after workaround. A really frustrating experience.

I must say, however, that it may work for simple APIs but even then usually only for generating the model part and not the API part. What kind of worked for me though was generating code for the server in form of a Kotlin interface for a Spring MVC controller. Although, here too, I had to modify the code generator templates to tailor it to my needs.

Regarding API client, when you can successfully generate an API client with code generation, it should be considered a low-level API client upon which one should build a high-level and more user-friendly API client (e.g. object model with actual methods and not only anemic objects).

Re: Ask HN: Best practices (and examples) for designing client libraries for APIs?

#15
This will only address part of your question, about API design.

I'd highly recommend Josh Bloch's writings. Josh is perhaps best known for the book Effective Java and the Java Collections library. Here is a paper he wrote about good API design (https://dl.acm.org/doi/abs/10.1145/1176617.1176622) and a short interview with him about API design (https://www.infoq.com/articles/API-Design-Joshua-Bloch/).

Brad Myers at CMU also has done research on API usability. See here for more details: http://www.cs.cmu.edu/~NatProg/apiusability.html

Lastly, if you're doing anything remotely related to security, I'd also recommend Matthew Smith's research. He's studied a lot about weaknesses of today's API designs and how they have led to security vulnerabilities. https://ieeexplore.ieee.org/abstract/document/7676144

Re: Ask HN: Best practices (and examples) for designing client libraries for APIs?

#16
I’m looking for a resource to gently pass to my client to help coach him up. He wrote all the server APIs, I write one of the mobile apps. His APIs are all massive JSON dumps of columns from the database where most of the fields have no use on mobile. The documentation is an out of date printout of the structures with comments, in Ruby I think.

Worse he doesn’t seem to do any data validation, I recently passed an index instead of an id for one field and ended up with tons of test data that’s broken. Not because of a bug in my code mind you, but because I misunderstood the cryptic comment.

Lastly, he occasionally asks the mobile apps to do processing of the data that’s more easily and safely done on the server. It’s like he misunderstands the role and value of the server in a client server application.

But again he’s my client so I have to be gentle.

Re: Ask HN: Best practices (and examples) for designing client libraries for APIs?

#17
post #11
post #6

Stripe has probably the best and cleanest API design.

And documentation. I would spend some time really understanding their docs and client libraries if you want to see a real world example of the most developer friendly docs available. https://stripe.com/docs/api https://stripe.com/docs/api/balance/balance_object?lang=pyth...

Is there any documentation of the client libraries themselves, or just the API?

Re: Ask HN: Best practices (and examples) for designing client libraries for APIs?

#18
Automated testing is a generically good technique for many things including forcing yourself to have a better API. Code sometimes has to be refactored to make it more testable, and this refactoring invariably implies a better API.

Write automated tests to hit each part of your API and test the functionality of each thing in isolation. If you find the tests frustrating to write, your users will find the API frustrating to use.

I've written more about this here:

https://bad-code-considered-harmful.blogspot.com/2020/05/tes...

By the way, I am not an advocate for TDD. I write my tests at the point when I'm ready to start running them. But I strongly agree with the general claim that TDD makes that says that good unit tests are the closest thing to a panacea you will ever encounter in software development. However, I've also seen projects where the unit tests were just more bloat. Unit tests need to be written to test your API to help you design a better API.

Re: Ask HN: Best practices (and examples) for designing client libraries for APIs?

#19
post #11

Earlier quoted context omitted.

And documentation. I would spend some time really understanding their docs and client libraries if you want to see a real world example of the most developer friendly docs available. https://stripe.com/docs/api https://stripe.com/docs/api/balance/balance_object?lang=pyth...

Is there any documentation of the client libraries themselves, or just the API?

Yes. It's integrated into the docs. If you look to the right you'll see code samples and you can choose different languages from a drop down.

https://stripe.com/docs/api/balance/balance_retrieve?lang=py...

The client libraries have stand alone documentation as well.

https://github.com/stripe/stripe-python

Re: Ask HN: Best practices (and examples) for designing client libraries for APIs?

#20
If you are lucky enough that your API vendor provides an OpenAPI (aka Swagger) specification (or possibly API Blueprint or RAML, if nothing else), make sure you build your client to follow the spec. Even if you are possibly aware of some undocumented features, try to avoid relying on them -- they might change or disappear without a notice.

I would also highly recommend to use the spec to dynamically construct your requests. I don't know if PHP has libraries to assist with that, but for example in Python you could use tools like Bravado or Pyotr client.

Post reply on HN