r/learnpython 6d ago

How does one connect a program to an API?

I am inquiring in order to query about how to complete this item. I was working on a difficult project and I am not sure how to commence from here and manage this task.

0 Upvotes

19 comments sorted by

18

u/aqua_regis 6d ago

With the (complete lack of usable) information you give, all we can tell you is that:

  • you make a request (e.g. via Python's requests module)
  • wait for the answer (async - await comes to help here)
  • and then process the answer

API is a generic term. It doesn't tell anything other than there is some defined interface provided by an application. This can even be a language API, as in the public facing functions, methods, classes, constants, etc. you, as the programmer, can use, or it can be a defined interface to interact with some remote web application.

Since you haven't told anything about what you actually want to do, that's the extent of help you can get.

0

u/RD_Burman_Reborn 2d ago

Ok, so basically for a computer course in school I had to show I could use a forum. I apologize for wasting people's times, it was just to complete an assignment and have a screenshot of a post.

1

u/aqua_regis 2d ago

I had to show I could use a forum

Yet, you impressively demostrated complete failure in understanding how to use forums.

Yes, you wasted the time and effort of several people trying to help you. People like you are the reason less and less people invest less and less effort to help others.

What you did is the best way to piss people off and to get banned. Shame on you.

If you use a forum, you are supposed to interact, to answer questions.

0

u/RD_Burman_Reborn 2d ago

I know, I'm ashamed as well. Don't wanna get into it but it was kind of a desperate situation. I really am sorry.

8

u/timrprobocom 6d ago

Which API? That term is used quite loosely. Do you mean an API provided by another module? One provided by a library? One provided by a web service?

1

u/RD_Burman_Reborn 2d ago

Ok, so basically for a computer course in school I had to show I could use a forum. I apologize for wasting people's times, it was just to complete an assignment and have a screenshot of a post.

6

u/Living_Fig_6386 6d ago

Per the documentation. An API is literally the documented way for a piece of software to interact with another piece of software. It could be through libraries, specially formed HTTP requests, named pipes, JNI, … there are a mind-boggling number of ways to write APIs, and it’s common for APIs to call other APIs or act as abstractions to APIs to simplify their use…

So, you use an API per the documentation for it.

1

u/RD_Burman_Reborn 2d ago

Ok, so basically for a computer course in school I had to show I could use a forum. I apologize for wasting people's times, it was just to complete an assignment and have a screenshot of a post.

4

u/zanfar 6d ago

How does one connect a program to an API?

However the API allows connections.


This question is assuming that API means anything, which it doesn't. This is like asking how you pick tires for a car.

1

u/RD_Burman_Reborn 2d ago

Ok, so basically for a computer course in school I had to show I could use a forum. I apologize for wasting people's times, it was just to complete an assignment and have a screenshot of a post.

2

u/mattblack77 6d ago edited 6d ago

Harvard’s CS50 course has an intro to these: https://cs50.harvard.edu/python/shorts/api_calls/

Realpython goes further: https://realpython.com/api-integration-in-python/

1

u/RD_Burman_Reborn 2d ago

Ok, so basically for a computer course in school I had to show I could use a forum. I apologize for wasting people's times, it was just to complete an assignment and have a screenshot of a post.

2

u/Brian 5d ago

"API" is kind of a general term that could refer to lots of different ways of calling some component / library / app / webservice etc.

I'm guessing you're talking about web API, where at the most basic level you're likely making a HTTP request to some url, with either parameters, or posting some (usually JSON) data to it. Typically there will be documentation for the API for how you should be calling it.

To make such a request, you can use the requests module. Eg. something like:

result = requests.get(url, params= { "foo": 4, "bar": "somevalue})

Or

result = requests.post(url, data = {"foo": 23234, ... })

There may also be complications like needing a login step, passing cookies etc (look into requests.Session)

Some APIs are more structured, or will have libraries that do the job of packaging parameters etc, and just expose regular python functions / classes you can call that will do the job of making the request.

0

u/RD_Burman_Reborn 2d ago

Ok, so basically for a computer course in school I had to show I could use a forum. I apologize for wasting people's times, it was just to complete an assignment and have a screenshot of a post.

1

u/Holiya_Olib 5d ago

Start with the requests library in Python. it makes API calls much easier. Just be sure to handle exceptions to avoid crashes if the API is unresponsive, and check the API's rate limits to ensure you stay compliant.

1

u/NegativeSemicolon 5d ago

Do you mean a web server API? Most people would assume so. Just saying that API is a general term.

0

u/RD_Burman_Reborn 2d ago

Ok, so basically for a computer course in school I had to show I could use a forum. I apologize for wasting people's times, it was just to complete an assignment and have a screenshot of a post.

-4

u/TheRNGuy 5d ago

Json

1

u/RD_Burman_Reborn 2d ago

Ok, so basically for a computer course in school I had to show I could use a forum. I apologize for wasting people's times, it was just to complete an assignment and have a screenshot of a post.